Convert Array to ArrayList

In this post, we will discuss 3 different ways of converting Array to ArrayList.
  1. Conversion using Arrays.asList()
  2. Collections.addAll method
  3. Conversion Manually

1. Conversion using Arrays.asList()

The Arrays class of the java.util package contains several static methods that we can use to fill, sort, search, etc in arrays. This class also contains a static factory that allows arrays to be viewed as lists.
Let's use Arrays built-in method asList() to convert Array to ArrayList.
Example: In this example, we will convert the String Array to ArrayList of String type.
import java.util.Arrays;
import java.util.List;

/**
 * This class shows different methods to convert Array to ArrayList
 * 
 * @author javaguides.net
 *
 */
public class ArrayToArrayList {

 public static void main(String[] args) {
  String anArrayOfStrings[] = { "Agra", "Mysore", "Chandigarh", "Bhopal" };

  List<String> strList = Arrays.asList(anArrayOfStrings);

  System.out.println("Original ArrayList from Arrays.asList()");

  /* Display array list */
  strList.forEach(str -> System.out.println(" " + str));

  // change the array element and see the effect is propogated to list
  // also.
  anArrayOfStrings[0] = "Dehli";

  System.out.println("\nChange in array effect on ArrayList");

  /* Display array list */
  strList.forEach(str -> System.out.println(" " + str));
 }

}
Output:
Original ArrayList from Arrays.asList()
 Agra
 Mysore
 Chandigarh
 Bhopal

Change in array effect on ArrayList
 Dehli
 Mysore
 Chandigarh
 Bhopal
Let's see one more example, convert Integer Array to ArrayList of Integer type.
Integer anArrayOfIntegers[] = { 1,2,3,4,5,6 };

List<Integer> intList = Arrays.asList(anArrayOfIntegers);

/* Display array list */
intList.forEach(str -> System.out.println(" " + str));
Output:
 1
 2
 3
 4
 5
 6

2. Collections.addAll method

addAll() method adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.
Example: In this example, we will convert the String Array to ArrayList of String type.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * This class shows different methods to convert Array to ArrayList
 * 
 * @author javaguides.net
 *
 */
public class ArrayToArrayList {

 public static void main(String[] args) {
  /* Array Declaration and initialization */
  final String array[] = { "abc", "xyz", "pqr", "XYZ" };

  /* ArrayList declaration */
  final ArrayList<String> arraylist = new ArrayList<String>();

  /* Conversion */
  Collections.addAll(arraylist, array);

  /* Adding new elements to the converted List */
  arraylist.add("String1");
  arraylist.add("String2");

  /* Display array list */
  arraylist.forEach(element -> System.out.println(" " + element));
 }
}
Output:
 abc
 xyz
 pqr
 XYZ
 String1
 String2
Let's see one more example, convert Integer Array to ArrayList of Integer type.
Integer anArrayOfIntegers[] = { 1,2,3,4,5,6 };
List<Integer> intList = new ArrayList<>();
Collections.addAll(intList, anArrayOfIntegers);

/* Display array list */
intList.forEach(element -> System.out.println(" " + element));

3. Conversion Manually

We can manually convert Array to ArrayList by looping over an array and adding each element to list.
import java.util.ArrayList;

/**
 * This class shows different methods to convert Array to ArrayList
 * 
 * @author javaguides.net
 *
 */
public class ArrayToArrayList {

 public static void main(String[] args) {
  /* Array Declaration and initialization */
  final String array[] = { "abc", "xyz", "pqr", "XYZ" };

  /* ArrayList declaration */
  final ArrayList<String> arraylist = new ArrayList<String>();

  for (int i = 0; i < array.length; i++) {
   arraylist.add(array[i]);
  }

  /* Display array list */
  arraylist.forEach(element -> System.out.println(" " + element));
  
  Integer anArrayOfIntegers[] = { 1,2,3,4,5,6 };

  /* ArrayList declaration */
  final ArrayList<Integer> intlist = new ArrayList<Integer>();

  for (int i = 0; i < anArrayOfIntegers.length; i++) {
   intlist.add(anArrayOfIntegers[i]);
  }
  /* Display array list */
  intlist.forEach(str -> System.out.println(" " + str));
 }

}
Output:
 abc
 xyz
 pqr
 XYZ
 1
 2
 3
 4
 5
 6

Related Array Posts

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments