Arrays.asList returns immutable List

I was having problems doing operations on a java.util.List object returned by Arrays.asList() method call. The specific exception was a java.lang.UnsupportedOperationException. I soon found out why this was a problem.

I like to use Arrays.asList as a nice one-line List creation tool. But, it turns out that many operations, such as add() and remove() are unusable on the List that is returned. The List returned is actually of type java.util.Arrays$ArrayList, a private static inner class of Arrays.

I was seeing this stack trace:

   at java.util.AbstractList.remove(
   at com.jtsnake.test.LettersBean.loadLetterOptions(
   at com.jtsnake.test.LettersBeanTest.testLoadLetterOptions(

So, if removals are something you want to do, this won't work:

List <LetterOption> list = Arrays.asList(new LetterOption());

Instead, you'll have to resort to the old-fashioned multi-liner:

List <LetterOption> list = new ArrayList < LetterOption > ();
list.add(new LetterOption());

I guess if you want flexible lists, you'll have to use Python after all.