Create ArrayList From Array

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to create an ArrayList from an array in Java. ArrayList is a dynamic array that can grow and shrink in size as required, and it belongs to the java.util package.

Create Project and File

Create a new Java file named ArrayListFromArr.java in the ~/project directory.

touch ~/project/ArrayListFromArr.java

Add Required Imports

To work with ArrayList, we need to import the java.util package. Add the following code at the top of the file in order to import the package:

import java.util.*;

Create an Array

Create an array of strings with some elements in it. We will use this array to create an ArrayList. Add the following code block within the main method:

String[] courses = {"Java", "Python", "C", "C++", "Ruby"};

Convert Array to ArrayList Explicitly

To create an ArrayList from the above-created array, we can add each element of the array to the ArrayList explicitly using a for loop. Add the following code block after the previous step:

ArrayList<String> courseList1 = new ArrayList<String>();
for (int i = 0; i < courses.length; i++) {
    courseList1.add(courses[i]);
}

Now compile and run the program using the following command:

javac ArrayListFromArr.java && java ArrayListFromArr

This will print the elements of courseList1 ArrayList on separate lines.

Convert Array to ArrayList with Arrays.asList()

Another way to create an ArrayList from an array is to convert the array to an ArrayList using the Arrays.asList() method. Add the following code block after the previous step:

ArrayList<String> courseList2 = new ArrayList<String>(Arrays.asList(courses));

Now compile and run the program using the following command:

javac ArrayListFromArr.java && java ArrayListFromArr

This will print the elements of courseList2 ArrayList on separate lines.

Convert Array to ArrayList with Collections.addAll()

We can also create an ArrayList from an array using the Collections.addAll() method. Add the following code block after the previous step:

ArrayList<String> courseList3 = new ArrayList<String>();
Collections.addAll(courseList3, courses);

Now compile and run the program using the following command:

javac ArrayListFromArr.java && java ArrayListFromArr

This will print the elements of courseList3 ArrayList on separate lines.

Print the Final Result

To print the final result, add the following code block after the previous step:

System.out.println("Course List 1: " + courseList1);
System.out.println("Course List 2: " + courseList2);
System.out.println("Course List 3: " + courseList3);

Now compile and run the program using the following command:

javac ArrayListFromArr.java && java ArrayListFromArr

This will print the elements of all three ArrayLists we created in previous steps.

Run the Program

Run the program using the following command:

javac ArrayListFromArr.java && java ArrayListFromArr

Final Code

Here is the final code:

import java.util.*;

public class ArrayListFromArr {

    public static void main(String[] args) {

        String[] courses = {"Java", "Python", "C", "C++", "Ruby"};

        // Method #1: Convert Array to ArrayList Explicitly
        ArrayList<String> courseList1 = new ArrayList<String>();
        for (int i = 0; i < courses.length; i++) {
            courseList1.add(courses[i]);
        }

        // Method #2: Convert Array to ArrayList with Arrays.asList()
        ArrayList<String> courseList2 = new ArrayList<String>(Arrays.asList(courses));

        // Method #3: Convert Array to ArrayList with Collections.addAll()
        ArrayList<String> courseList3 = new ArrayList<String>();
        Collections.addAll(courseList3, courses);

        // Print the results
        System.out.println("Course List 1: " + courseList1);
        System.out.println("Course List 2: " + courseList2);
        System.out.println("Course List 3: " + courseList3);

    }

}

Save and exit the file.

Summary

In this lab, we learned how to create an ArrayList from an array in Java. We saw three different ways to do so, namely, by adding each element of the array to ArrayList explicitly, by using Arrays.asList() method, and by using Collections.addAll() method. We also learned how to print the result using System.out.println() method.

Other Java Tutorials you may like