Arrays.asList()返回的是一个内部类,可以对数组进行值的修改
但是,如果涉及到数组数量的修改,就会报错,因为它数组指向的还是原数组
public class Main6 {
public static void main(String[] args) {
String[] stringArray = new String[3];
stringArray[0] = "one";
stringArray[1] = "two";
stringArray[2] = "three";
//Arrays.asList()返回的是一个内部类,可以对数组进行值的修改
//但是,如果涉及到数组数量的修改,就会报错,因为它数组指向的还是原数组
List<String> stringList = Arrays.asList(stringArray);
stringList.set(0,"one-list");
System.out.println(stringArray[0]);
//看这3个操作,如果涉及到数组数量的修改,就会报错,因为它数组指向的还是原数组
stringList.add("four");
stringList.remove(2);
stringList.clear();
}
}
public class Main6 {
public static void main(String[] args) {
List masterList = new ArrayList();
masterList.add("one");
masterList.add("two");
masterList.add("three");
masterList.add("four");
masterList.add("five");
List branchList = masterList.subList(0, 3);
//这3行不注释会导致报错
//masterList.remove(0);
//masterList.add("ten");
//masterList.clear();
branchList.clear();
branchList.add("six");
branchList.add("seven");
branchList.remove(0);
//此时 branchList 只剩下一个元素 seven
//masterList 剩下3个元素,seven four five
}
}