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

import java.util.*;
public class ArrayToArrayList
{
  public static void main(String args[])
  {
    String goodWords[] = { "corruption", "stops", "country's", "growth" }; 
	
			        // ARRAY TO LIST - FIRST STYLE                                                
    ArrayList al1 = new ArrayList(goodWords.length);
    for(String str : goodWords)
    {
      al1.add(str);
    }
    System.out.println("Array elements: " + Arrays.toString(goodWords)); 
    System.out.println("ArrayList elements with for loop: " + al1);

				// ARRAY TO LIST - SECOND STYLE                                           
    ArrayList al2 = new ArrayList(goodWords.length);   
    Collections.addAll(al2, goodWords);
    System.out.println("ArrayList elements with addAll(): " + al2);

				// ARRAY TO LIST - THIRD STYLE
    List al3 = new ArrayList(Arrays.asList(goodWords));   
    System.out.println("ArrayList elements with asList(): " + al3);
				
				// ANOTHER STYLE OF USING ASLIST()
    List al4 = Arrays.asList(goodWords);
    System.out.println("List elements with asList(): " + al4);
  }
}

Java Convert Array ArrayList

     String goodWords[] = { "corruption", "stops", "country's", "growth" }; 
     ArrayList al1 = new ArrayList(goodWords.length);
     for(String str : goodWords)
     {
       al1.add(str);
     }

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.

    
     ArrayList al2 = new ArrayList(goodWords.length);   
     Collections.addAll(al2, goodWords);

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.

      List al3 = new ArrayList(Arrays.asList(goodWords));   

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.

        List al4 = Arrays.asList(goodWords);
        System.out.println("List elements with asList(): " + al4);

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()

     List al4 = Arrays.asList(goodWords);

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()

    
     ArrayList al2 = new ArrayList(goodWords.length);   
     Collections.addAll(al2, goodWords);

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 Comment

Your email address will not be published.