背景:
今天在跑一个UnitTest,跑的过程中想在list的最后多加一个Element,即 List.add(Element e),多测试一条数据。 可是在run的过程中,却一直在抛:Caused by: java.lang.UnsupportedOperationException。 我对这个异常不了解,凭借自己的有限知识,都不能解决这个问题/最后google到了答案,先上link:
http://craftingjava.blogspot.com/2012/06/how-to-resolve-unsupportedoperationexce.html。
方案:
首先要知道这个是什么:
了解什么是UnsupportedOperationException, 只有知道了它,我们才能更好的来解决这个问题。 官方有个解释是:
Throws:
UnsupportedOperationException - if the add operation is not supported by this list,
也就是说add操作对此list来说,不被支持了。 那么什么情况才不被支持呢?
也就是为什么:
UnsupportedOperationException异常的发生通常都是在集合框架中,例如:List,Queue,Set,Map等等。针对这些集合,我们要知道它是分成不同的type的,一类就可以被修改的,一个就是不能被修改的(就是相当于这个值是固定的,不能被加减)。 也就是link文件里提到的view的概念, 也就是view是read-only 的。
引用
A view is a read-only format of the collections,which means that through view we can traverse the collections and even we can retrieve values.But if you try to modify the collection using view object this will cause an UnsupportedOperationException to be thrown.
也就是说Lists.asList()得到的list是跟new ArrayList() 是不一样的,new出来的List是可以随意add,remove的但是Lists.asList得到的却不能这么玩。这个要看具体的api,例如: List是不能用List.remove(index) 来操作的,但是Map.remove(Key)却不报错。
参考如下代码:
public static void main(String[] args) {
Person person = new User();
List<Person> list = new ArrayList<Person>();
list.add(person);
String s[]={"ram","ganesh","paul"};
List li=Arrays.asList(s);
li.remove(0);
Map map =new HashMap();
map.put("1","Ram");
map.put("2","Ganesh");
map.put("3","Paul");
System.out.println(map);
map.remove("1");
System.out.println(map);
}
现在知道UnsupportedOperationException 异常怎么改了吧:)
----EOF----
分享到:
相关推荐
Caused by: java.lang.OutOfMemoryError: PermGen space解决方案, avax.servlet.ServletException: java.lang.OutOfMemoryError: PermGen space at org.apache.jasper.servlet.JspServlet.service(JspServlet.java...
Caused by: java.net.UnknownHostException: openapi.alipay.com
整个系统都没有错误,但是在发布运行后,控制台上却抛出了这样的异常: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
Caused by: android.system.ErrnoException: write failed: ENOSPC (No space lef
主要介绍了Android Caused by: java.lang.ClassNotFoundException解决办法的相关资料,需要的朋友可以参考下
主要介绍了Android 兼容性问题:java.lang.UnsupportedOperationException解决办法的相关资料,需要的朋友可以参考下
异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract ...
主要介绍了Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://redarmychen.iteye.com/blog/463864
java.lang.NoSuchFieldError: Companion 问题的解决方案
Caused by: java.lang.NoClassDefFoundError: com.sun.org.apache.xml.internal.resolver.CatalogManager at com.sun.xml.ws.util.xml.XmlUtil.createEntityResolver(XmlUtil.java:266) at org.jvnet.jax_ws_...
NULL 博文链接:https://geyubin.iteye.com/blog/779330
java解决nested exception is java.lang.OutOfMemoryError Java heap space 解决OOM
java 9 中引入了模块的概念,默认情况下,Java SE中将不再包含java EE 的Jar包 而在 java 6/7 / 8 时关于这个API 都是捆绑在
java.lang.NumberFormatException For inputstring 4294967295处理方法
java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport异常处理
<groupId>com.squareup.okio</groupId> <artifactId>okio <version>2.8.0</version>
主要介绍了启动Azkaban报错:java.lang.NoSuchMethodError: com.google.common.collect.ImmutableMap.toImmutableMap,需要的朋友可以参考下
Caused by: java.sql.SQLException: java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver at com.trs.gateway.commons.hive.HiveFeature.getConnection(HiveFeature.java:57) at ...