How to convert an array to ArrayList in java

In The previous post we have learn difference ways of conversion arraylist to array in java . Now in this tutorial we will learn How to convert array to list java . We have different ways for this as following

  1. Arrays.asList()
  2. Collections.addAll()
  3. add() method

Let’s discuss these way one by one

1. Conversion using Arrays.asList()

syntax:

/* 
Returns a fixed-size list backed by the specified array
*/
public static <T> List<T> asList(T... a) {
        
    }

This method take array argument and return list which have

  • Returns a fixed-size list backed by the specified array
  • The returned list is serializable and implements RandomAccess.
  • We cann’t add more element in this list because it is fixed size list
import java.util.Arrays;
import java.util.List;

public class ArrayToArrayList {

	public static void main(String[] args) {
		String [] arr = {"java","php" ,"spring","mysql","android"};
		/*
		 * Returns a fixed-size list backed by the specified array.
		 */
		List<String> list = Arrays.asList(arr);
		System.out.println("list is = "+ list);
		
	}
	
}

Output:

list is = [java, php, spring, mysql, android]

Now if we add element in returned list we will get error “java.lang.UnsupportedOperationException”

public class ArrayToArrayList {

	public static void main(String[] args) {
		String [] arr = {"java","php" ,"spring","mysql","android"};
		/*
		 * Returns a fixed-size list backed by the specified array.
		 */
		List<String> list = Arrays.asList(arr);
		
		//print list
		System.out.println("list is = "+ list);
		
		list.add("angularjs");
		
		//print list
		System.out.println("list is ="+ list);
		
	}
	
}

Output :

list is = [java, php, spring, mysql, android]
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at com.demo.arraylist.ArrayToArrayList.main(ArrayToArrayList.java:18)

Therefore It is recommended that create arraylist using new arraylist and pass Arrays.asList(arr) as argument in constructor of arraylist . Let’s see code for that

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayToArrayList {

	public static void main(String[] args) {
		String [] arr = {"java","php" ,"spring","mysql","android"};
		/*
		 * Returns a fixed-size list backed by the specified array.
		 */
		List<String> list = new ArrayList<String>(Arrays.asList(arr));
		
		//print list
		System.out.println("list is = "+ list);
		
		list.add("angularjs");
		
		//print list
		System.out.println("list is ="+ list);
		
	}
	
}

Output :

list is = [java, php, spring, mysql, android]
list is =[java, php, spring, mysql, android, angularjs]

 2. Using Collections.addAll() Method

Syntax :

 /*
Adds all of the specified elements to the
specified collection.
*/
public static <T> boolean addAll(Collection<? super T> c, T... elements) {

}

Collections.addAll() method Adds all of the given elements to the specified collection. Elements to be added may be specified individually or as an array. This method provides a convenient way to add a few elements to an existing collection:
Collections.addAll(list, “java”, “spring”);

it is returns  true if the collection changed as a result of the call
and it throws UnsupportedOperationException – if collection c does not support the add operation
NullPointerException – if elements contains one or more null values and collection c does not permit null elements, or if c or elements are null
IllegalArgumentException – if some property of a value in elements prevents it from being added to collection c. Now we will convert array to arraylist by using this method .

import java.util.ArrayList;
import java.util.Collections;

public class ArrayToListByAllAdd {

	public static void main(String[] args) {
	
		String [] arr = {"java","php" ,"spring","mysql","android"};
		
		ArrayList<String> arrayList = new ArrayList<String>();

		/*
            Adds all of the specified elements to the
             specified collection.
		 */
		Collections.addAll(arrayList,arr);
	
		System.out.println("list is ="+ arrayList);

	}
	
}

Output :

list is =[java, php, spring, mysql, android]

3. Manual method to convert Array using add() method

In this method we will iterate array and one by one all element add to arraylist using add() method

import java.util.ArrayList;

public class ArrayToListByAdd {

	public static void main(String[] args) {
        String [] arr = {"java","php" ,"spring","mysql","android"};
		
		ArrayList<String> arrayList = new ArrayList<String>();
		
		//add array element in arraylist one by one using add()
		for (int i = 0; i < arr.length; i++) {
		      arrayList.add(arr[i])	;
		}
		
		//print arraylist
		System.out.println("list is ="+arrayList);
		
	}
	
}

Output :

list is =[java, php, spring, mysql, android]

 

Leave a Reply

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

80 − 76 =