import java.util.*;
public class test11 {
public static void main(String[] args) {
List<Integer> arr =new ArrayList<>(5);
System.out.println("1: "+arr);
arr.add(0,12);
arr.add(1,45);
arr.add(2,63);
arr.add(3,47);
arr.add(4,96);
//arr.add(3,24); 如果一开始直接add索引3,会出错
//Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
//超出范围
List sub=arr.subList(1,3);
System.out.println("2: "+sub);
arr.addAll(3,sub);
System.out.println("3: "+arr); //从索引3后面加入另外整个List
System.out.println("4: "+arr.contains(45)+" "+arr.contains(58));
arr.add(arr.size(),11);
System.out.println("5: "+arr);
System.out.println("6: "+arr.indexOf(47));
arr.remove(2);
System.out.println("7: "+arr);
List sub2= arr.subList(1,5);
System.out.println("8: "+arr.containsAll(sub2)+" "+sub2.containsAll(arr));
//System.out.println("8: "+arr.containsAll(sub));
Collections.sort(arr);
System.out.println("9: "+arr);
ArrayList copy=new ArrayList<>(arr);
System.out.println("10: "+copy);
Collections.shuffle(arr);
System.out.println("11: "+arr+" "+copy);
int[] a2=new int[]{11,22,33,45,96};
List arr2=Arrays.asList(11,22,33,45,96);
//List arr2=Arrays.asList(a2); //这里是把a2这个数组当成一个int[]类型整个传入 转化成一个只有一个元素的List
System.out.println("12: "+arr2);
arr.retainAll(arr2);
System.out.println("13: "+arr);
System.out.println("14: "+arr.isEmpty());
arr.removeAll(arr2);
System.out.println("15: "+arr+" "+arr.isEmpty());
}
}
网友评论