Initialize ArrayList of array

  1. HowTo
  2. Java Howtos
  3. Create ArrayList From Array in Java

Create ArrayList From Array in Java

Java Java Array Java ArrayList

Created: October-01, 2020 | Updated: October-17, 2021

This tutorial article will introduce different ways to create ArrayList from array in Java. There are three different methods to convert an array to ArrayList in Java such as Arrays.asList(), Collections.addAll() and add().

Before proceeding with the demonstration, let us understand what is an array and ArrayList and how they differ from each other.

What Is an Array in Java?

An array is a collection of a fixed number of similar types of data. For example, if we want to store the data of 50 books, we can create an array of the string type that can hold 50 books. After creation, the length of the array is fixed. An array is the basic built-in functionality of Java.

String[] array = new String[50];

What Is ArrayList in Java?

The ArrayList is a resizable array that stores a dynamic collection of elements found within the java.util package.

Difference Between Array and ArrayList in Java

The main difference between an array and ArrayList is that the length of an array cannot be modified or extended. To add or remove elements to/from an array, we have to create a new list. Whereas, elements can be added or removed to/from ArrayList at any point due to its resizable nature.

Conversion of an Array to ArrayList Using Arrays.asList()

Using Arrays.asList(), the array is passed to this method and a list object is obtained, which is again passed to the constructor of the ArrayList class as a parameter. The syntax of the Arrays.asList() is as below:

ArrayList arraylist = new ArrayList(Arrays.asList(arrayname));

Let us follow the below example.

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Method1 { public static void main(String[] args) { String[] subjects = {"maths", "english", "history", "physics"}; ArrayList sublist = new ArrayList(Arrays.asList(subjects)); sublist.add("geography"); sublist.add("chemistry"); for (String str: sublist) { System.out.println(str); } } }

Output:

maths english history physics geography chemistry

Conversion of an Array to ArrayList Using Collections.addAll()

This method lists all the array elements in a definite collection almost similar to Arrays.asList(). However, Collections.addAll() is much faster as compared to Arrays.asList() method on performance basis. The syntax of Collections.addAll() is as below:

Collections.addAll(arraylist, new Element(1), new Element(2), new Element(3), new Element(4));

Let us understand the below example.

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Method2 { public static void main(String[] args) { String[] names = {"john", "emma", "rick", "tim"}; ArrayList namelist = new ArrayList(); Collections.addAll(namelist, names); namelist.add("jenny"); namelist.add("rob"); for (String str: namelist) { System.out.println(str); } } }

Output:

john emma rick tim jenny rob

Conversion of an Array to ArrayList Using add()

Using this method, we can create a new list and add the list elements in a much simpler way. The syntax for the add() method is as below:

arraylist.add(element);

Let us check the below example.

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Method3 { public static void main(String[] args) { ArrayList planetlist = new ArrayList(); String[] planets = {"earth", "mars", "venus", "jupiter"}; for(int i=0;iOutput:

earth mars venus jupiter

Following the above methods, we can now easily convert an array to ArrayList.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Array

  • Increase an Array Size in Java
  • Print an Array in Java

    Related Article - Java ArrayList

  • Create a Subarray in Java
  • Convert an Arraylist to a String in Java
    • Convert Long to Int in Java
    • Convert Character to ASCII Numeric Value in Java
    Initialize ArrayList of array
    report this ad