Java int[] arraylist

Asked

Viewed 87 times

5

I’m creating a list from a array using the method Arrays.asList.

Now I want to remove the first element, but Java accuses:

Exception in thread "main" java.lang.Unsupportedoperationexception

List<Integer> lista = Arrays.asList(new Integer[] { 1, 2, 3 } );
lista.remove(0);

1 answer

7


The method Arrays.asList returns a fixed-size list. See documentation.

That is, you cannot add elements to it, you cannot remove elements from it and you cannot touch its structure.

You can simply create a ArrayList from this list returned by the method Arrays.asList.

List<Integer> lista = new ArrayList<>(Arrays.asList(new Integer[] { 1, 2, 3 } ));        
lista.remove(0);

See working on repl.it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.