UnsupportedOperationException异常

UnsupportedOperationException异常

本文介绍了Java的List.add()UnsupportedOperationException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在列表与LT添加对象;串> 实例,但它抛出UnsupportedOperationException。任何人都知道这是为什么?

我的Java code:

 的String [] = membersArray request.getParameterValues​​('会员');
清单<串GT; membersList = Arrays.asList(membersArray);对于(string成员:membersList){
    人人= Dao.findByName(会员);
    清单<串GT;也可以看看;
    seeAlso = person.getSeeAlso();
    如果(!seeAlso.contains(groupDn)){
        seeAlso.add(groupDn);
        person.setSeeAlso(seeAlso);
    }
}

错误消息:


 java.lang.UnsupportedOperationException
    java.util.AbstractList.add(来源不明)
    java.util.AbstractList.add(来源不明)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


解决方案

不是每一个的实现支持添加()方法。

一个常见的​​例子是列表按<$c$c>Arrays.asList():它记录的,以支持任何结构上的修改(即删除或添加元素)(重点煤矿):

Even if that's not the specific List you're trying to modify, the answer still applies to other List implementations that are either immutable or only allow some selected changes.

You can find out about this by reading the documentation of UnsupportedOperationException and List.add(), which documents this to be an "(optional operation)". The precise meaning of this phrase is explained at the top of the List documentation.

As a workaround you can create a copy of the list to a known-modifiable implementation like ArrayList:

seeAlso = new ArrayList<>(seeAlso);

这篇关于Java的List.add()UnsupportedOperationException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:06