0

Array-backed Lists in Java

In Java, there are these array-backed lists that are generated when you convert arrays to lists using Arrays.asList(...). The list and the array objects point to the same data stored in the heap. Changes to the existing contents through either the list or array result to changing the same data.

For instance, we have these codes which were taken from Converting Between array and List in Java.

Array to List

Notice this code:

It creates a array-backed list. Any change to the current data, will be reflected on the original array names. Let’s modify the codes a bit to show this “phenomena”.

Test the effects

In line 13, we changed the value of the first element in the list. Not in the array. However, the original array is also modified.

List becomes fixed-length

Array-backed list are fixed-length. You cannot change the list by adding or removing elements.

If you try to, java.lang.UnsupportedOperationException is thrown.

Try adding “George” in the list and the codes output the following.