需求
在思考下面这个题的时候,用到了数组和list,在数组转list,调用add方法的时候报错
String res = "aaa,bbb,ccc"; String[] split = res.split(","); List<String> list = Arrays.asList(split); list.add("ddd");
报错信息如下:其实就是AbstractList类的add方法抛出的
点asList,可以看到是创建了一个ArrayList对象,点ArrayList
定位到下图的ArrayList类(注意,这里是Arrays类中的一个内部类,和平时我们常用的ArrayList类不一样),父类是AbstractList
查看这个内部类的方法add:前面两个是类的方法,后面两个是接口的方法
点第一个add方法
定位到父类AbstractList中add方法,方法里面调用了另外一个2个参数的add方法
点下图add
定位到两个参数的add方法,这里通过throw在方法内抛出异常
结论:通过Arrays.asList()方法创建的list,并不支持list的add操作,其它方法(比如remove)也一样。
- UnsupportedOperationException quot Exception 源码 threadunsupportedoperationexception quot exception outofmemoryerror quot exception thread unsatisfiedlinkerror quot exception thread classnotfoundexception quot exception thread unsupportedoperationexception java exception assertionerror exception thread main quot exception rabbitmq reason bindingexception quot exception statement messagingexception quot exception connect version unsupportedclassversionerror quot exception