JAVA编程思想的第十一章持有对象中有这样一个问题。
其代码如下(在寻找错误原因的时候,我对代码已进行了少量修改):
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class AddGroups {
public static void main(String[] aaa) {
Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer[] moreInt = {6,7,8,9,10};
Collections.addAll(collection, moreInt);
Collections.addAll(collection, 11,12,13,14,15);
List<Integer> list = Arrays.asList(16,17,18,19,20);
list.set(1, 99);
// ((ArrayList<Integer>)list).add(3);
list.add(3);
List<Integer> list2 = new ArrayList<Integer>();
list2.add(30);
System.out.println("list究竟是什么?");
System.out.println(list);
System.out.println(list.getClass());
for(Integer i:list2){
System.out.print(i+" ");
}
System.out.println("abc");
for(Integer i:collection) {
System.out.print(i+" ");
}
}
}
这段代码在Eclipse上运行的时候会提示如下错误:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.AbstractList.add(AbstractList.java:153)
at java.base/java.util.AbstractList.add(AbstractList.java:111)
at