1. 程式人生 > 其它 >我無法在列表中新增元素? UnsupportedOperationException異常(I am unable to add an element to a list? UnsupportedOperationException)

我無法在列表中新增元素? UnsupportedOperationException異常(I am unable to add an element to a list? UnsupportedOperationException)

轉自:https://www.it1352.com/968919.html

This one list object is biting me in the butt..

Any time I try to add an element to it, it produces this:

Caused by: java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:148)
        at java.util.AbstractList.add(AbstractList.java:108)

The line producing the error is insignificant, but here it is anyways:

AdventureLobbies.players.add(args[0].toLowerCase());

Should I not be accessing it statically?

Actual declaration of variable:

AdventureLobbies.players = Arrays.asList(rs.getString("players").toLowerCase().split(","));

Any ideas? Can't find anything on Google that's worthwhile.

解決方案

Arrays.asList() will give you back an unmodifiable list, and that is why your add is failing. Try creating the list with:

AdventureLobbies.players = new ArrayList(Arrays.asList(rs.getString("players").toLowerCase().split(",")));

這一個列表物件正在咬我... ...



每當我嘗試新增一個元素時,它就會產生這個:



 由以下原因引起:java.lang.UnsupportedOperationException 
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)


產生錯誤的行是無關緊要,但無論如何:



  AdventureLobbies.players.add(args [0] .toLowerCase()); 


我是否應該靜態訪問它?



< p>變數的實際宣告:

AdventureLobbies.players = Arrays.asList(rs.getString("players")。toLowerCase()。split (","));



任何想法?在谷歌上找不到任何值得的東西。