Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding to front of List throws?

Tags:

java

list

Trying to use the following to add to the front of a List but throws on the add. The doc says it's supposed to shift. What is the fix or workaround?

List<String> whatever = Arrays.asList("Blah1", "Blah2");
whatever.add(0, "BlahAll"); // <- Throws
like image 852
genxgeek Avatar asked Dec 01 '22 18:12

genxgeek


1 Answers

You can't add element to a list created using Arrays.asList(). As the documentation explains:

Returns a fixed-size list backed by the specified array.

like image 148
LaurentG Avatar answered Dec 10 '22 12:12

LaurentG