Java Convert Array to ArrayList

Java Convert Array to ArrayList

Java Convert Array ArrayList

Summary: It is quiet common in Java coding to convert array to array list and vice versa. 4 styles are given in simple terms, examples in this tutorial "Java Convert Array ArrayList".

Following are the methods signature used Java Convert Array ArrayList.

1. Defined in Array class

  • static List asList(T array): It is useful to convert an array into a List. The method returns a List object with the elements of the array and further elements cannot be added to the list. Useful to have fixed size list on which add() and remove() methods do not work. Overloaded to take any data type array as parameter.

2. Defined in Collections class

  • static boolean addAll(Collection col1, Object obj1): Here, obj1 can be a single element or an array. Adds obj1 to the Collection col1. The same method also exists in Collection and List interfaces.

To convert array to array list four styles are given in the following program and everyone comes with its own plus and minus points.

Example on Java Convert Array ArrayList

Java Convert Array ArrayList

In the above code, a string array goodWords is created with 4 elements. An array list object al1 is also created sufficient to store the 4 elements (to avoid excess memory utilization). Using enhanced for loop each element of the array is added to array list. In this style, the array and array list are independent of each other. Further elements can be added to the array or array list or removed as per the programmer's convenience. But next styles are not like this.

Using addAll() method of Collections class, all the array elements are added to array list al2. In this code also, both the array and array list are independent of each other. Further elements can be added to the array or array list as per the programmer's convenience. Usage of addAll() method avoids programmer's manual coding of copying elements as is done in the earlier style.

This style of adding elements to array list, using asList() method of Arrays class, is some times needed as the list object al3 returned permits to add further elements. It is the shortest when compared with the earlier two. But the next asList() method does not permit.

The List object al4 returned by asList() method of Arrays class does not permit to add or remove elements. It is read-only list. This is convenient when the programmer would like an unmodifiable list that avoids accidental erasure of elements.

Which is efficient asList() or addAll()?

Even though both get an object of List, they different in their features.

1. Arrays.asList()

asList() method returns a List object that represents a view of the array and functions as a wrapper that makes array looks like a list.

The List al4 returned by asList() method is of fixed size. That is, you cannot add more elements or remove elements and when tried throws UnsupportedOperationException.

This is very efficient by performance-wise as it does not copy the elements of the array into the list. Here al4 works as a wrapper for goodWords array. This style can be chosen when the programmer requires a read-only or fixed size list.

2. Collections.addAll()

This style is not efficient as array elements are copied into the ArrayList, al2. But is more flexible as the ArrayList al2 is not of fixed size and new elements can be added or modified or removed.

Two more programs are available at "Converting array into List with asList()" and "Adding Array to Collections classes with addAll()" for your further reading.

Array related topics that increase array manipulation techniques

Leave a Reply

Your email address will not be published. Required fields are marked *