Java, Software Development

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 an 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

On 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

The array-backed list is 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 will output the following.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like