Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

java - I am unable to add an element to a list? UnsupportedOperationException

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(",")));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...