码迷,mamicode.com
首页 > 其他好文 > 详细

List进行remove和add操作时缘何会报UnsupportedOperationException

时间:2015-05-22 11:31:17      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:list   unsupportedoperation   

看段简单的代码:

String[] arry={"abc","eee","fff"};
List<String> list= Arrays.asList(arry);
list.add("eeef");

运行结果:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at com.jd.etms.vrs.ws.test.TestList.main(TestList.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

原因是:使用Arrays.asList转换过来的集合是固定长度的,不能进行remove和add

解决办法:

 List<String> list= new ArrayList<String>(Arrays.asList(arry));

List进行remove和add操作时缘何会报UnsupportedOperationException

标签:list   unsupportedoperation   

原文地址:http://blog.csdn.net/wantken/article/details/45914343

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!