常常使用Arrays.asLisvt()后调用add,remove这些method时出现java.lang.UnsupportedOperationExce ption异常。这是由于:
Arrays.asLisvt() 返回java.util.Arrays$ArrayList,而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,remove,add等method在AbstractList中是默认throwUnsupportedOperationExce ption而且不作任何操作。ArrayListoverride这些method来对list进行操作,但是Arrays$ArrayList没有overrideremove(int),add(int)等,所以throw UnsupportedOperationExce ption。
解决方法是使用Iterator,或者转换为ArrayList
List<String> aList = Arrays.asList(new String[] {"1","2","3","6"});
List<String> bList = Arrays.asList(new String[] {"1","2","3"});
List<String> arrayList = new ArrayList<String>(aList);
boolean a = arrayList.removeAll(bList);
System.out.println(a);//true
System.out.println(Arrays.toString(arrayList.toArray()));//6
Arrays.asLisvt() 返回java.util.Arrays$ArrayList,而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,remove,add等method在AbstractList中是默认throwUnsupportedOperationExce
解决方法是使用Iterator,或者转换为ArrayList
List<String> aList = Arrays.asList(new String[] {"1","2","3","6"});
List<String> bList = Arrays.asList(new String[] {"1","2","3"});
List<String> arrayList = new ArrayList<String>(aList);
boolean a = arrayList.removeAll(bList);
System.out.println(a);//true
System.out.println(Arrays.toString(arrayList.toArray()));//6