Arrays asList typecast

Difference between Arrays.asList[array] and new ArrayList[Arrays.asList[array]]

  1. First, let's see what this does:

    Arrays.asList[ia]

    It takes an array ia and creates a wrapper that implements List, 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. ArrayLists 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.

  2. On the other hand, if you subsequently create a new array as

    new ArrayList[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.


Well this is because ArrayList resulting from Arrays.asList[] is not of the type java.util.ArrayList . Arrays.asList[] creates an ArrayList of type java.util.Arrays$ArrayList which does not extend java.util.ArrayList but only extends java.util.AbstractList


List list1 = new ArrayList[Arrays.asList[ia]]; //copy

In this case, list1 is of type ArrayList.

List list2 = Arrays.asList[ia];

Here, the list is returned as a List view, meaning it has only the methods attached to that interface. Hence why some methods are not allowed on list2.

ArrayList list1 = new ArrayList[Arrays.asList[ia]];

Here, you ARE creating a new ArrayList. You're simply passing it a value in the constructor. This is not an example of casting. In casting, it might look more like this:

ArrayList list1 = [ArrayList]Arrays.asList[ia];

Video liên quan

Chủ Đề