What is the difference between
1.List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(ia)); //copy
2.List<Integer> list2 = Arrays.asList(ia);
where ia
is array of integers.
I came to know that some operations are not allowed in list2
. why is it so?
how is it stored in memory (references / copy)?
When I shuffle the lists, list1
doesn't affect the original array but list2
does. But still list2
is somewhat confusing.
How ArrayList
being upcasted to list differs from creating new ArrayList
list1 differs from (1)
ArrayList<Integer> list1 = new ArrayList<Integer>(Arrays.asList(ia));
First, let's see what this does:
Arrays.asList(ia)
It takes an array ia
and creates a wrapper that implements List<Integer>
, which makes the original array available as a list. Nothing is copied and all, only a single wrapper object is created. Operations on the list wrapper are propagated to the original array. This means that if you shuffle the list wrapper, the original array is shuffled as well, if you overwrite an element, it gets overwritten in the original array, etc. Of course, some List
operations aren't allowed on the wrapper, like adding or removing elements from the list, you can only read or overwrite the elements.
Note that the list wrapper doesn't extend ArrayList
- it's a different kind of object. ArrayList
s have their own, internal array, in which they store their elements, and are able to resize the internal arrays etc. The wrapper doesn't have its own internal array, it only propagates operations to the array given to it.
On the other hand, if you subsequently create a new array as
new ArrayList<Integer>(Arrays.asList(ia))
then you create new ArrayList
, which is a full, independent copy of the original one. Although here you create the wrapper using Arrays.asList
as well, it is used only during the construction of the new ArrayList
and is garbage-collected afterwards. The structure of this new ArrayList
is completely independent of the original array. It contains the same elements (both the original array and this new ArrayList
reference the same integers in memory), but it creates a new, internal array, that holds the references. So when you shuffle it, add, remove elements etc., the original array is unchanged.