Difference between ArrayList and Arrays.asList() in Java

2015-12-01 14-17-02 by Kamushin

Arrays.asList does not return an ArrayList instance which has an add method implemented correctly.
It returns a List that is a "view" onto the array - a wrapper that makes the array look like a list
Changes to the returned list write through to the array
Since the returned instance is a fixed-size array, the add method always throw an UnsupporedOperationException.
If want to use add method

  new ArrayList(Arrays.asList(myArray)); //copies the content of the array to a new ArrayList 

Comments