用 Arrays.asList() 生产的对象不能添加和删除元素,因为对象的类型是 java.util.Arrays 的内部类 ArrayList,不是 java.util.ArrayList
List<String> list = Arrays.asList("墨倾池", "砚寒清", "默苍离", "慕容宁");
list.remove(3);
System.out.println(list);
输出
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at org.example.runoob.high.collectionsframework.arraylist.Main.testRemove(Main.java:49)
at org.example.runoob.high.collectionsframework.arraylist.Main.main(Main.java:13)