java.lang.UnsupportedOperationException

java.lang.UnsupportedOperationException

代码部分:

public static void main(String[] args) {  
        String[] arr ={"a","b","c"};  
        List list = Arrays.asList(arr);  
        list.add("d"); 
        list.add("e");  
    } 

运行报出的异常:

Exception in thread "main" java.lang.UnsupportedOperationException  
        at java.util.AbstractList.add(AbstractList.java:131)  
        at java.util.AbstractList.add(AbstractList.java:91)  
        at com.cn.test.main(test.java:11)  

解决办法:
List arrayList =new ArrayList(a);

public static void main(String[] args) {  
        String[] arr ={"a","b","c"};  
        List list = Arrays.asList(arr);  
        List arrList = new ArrayList(list);  
        arrList.add("d");  
        arrList.add("e");  
    }

查明的原因:
在使用Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常。这是由于Arrays.asList() 返回java.util.Arrays ArrayListArrayListArrays ArrayList和ArrayList都是继承AbstractList,remove,add等method在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。ArrayList override这些method来对list进行操作,但是Arrays$ArrayList没有override remove(),add()等,所以throw UnsupportedOperationException。

转载自:http://blog.csdn.net/thunderous/article/details/3693362

你可能感兴趣的:(java)