Arraylist of int [] in java

5

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

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);
    
asked by anonymous 17.07.2017 / 22:22

1 answer

7

The method Arrays.asList returns a fixed-length list . View Documentation .

That is, you can not add elements to it, you can not remove elements from it, and you can not tweak the structure of it.

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

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

See working on repl.it.

    
17.07.2017 / 22:33