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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/generics("`Generics`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/arraylist("`ArrayList`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117403{{"`Create ArrayList From Array`"}} java/generics -.-> lab-117403{{"`Create ArrayList From Array`"}} java/arraylist -.-> lab-117403{{"`Create ArrayList From Array`"}} java/classes_objects -.-> lab-117403{{"`Create ArrayList From Array`"}} java/class_methods -.-> lab-117403{{"`Create ArrayList From Array`"}} java/modifiers -.-> lab-117403{{"`Create ArrayList From Array`"}} java/oop -.-> lab-117403{{"`Create ArrayList From Array`"}} java/identifier -.-> lab-117403{{"`Create ArrayList From Array`"}} java/arrays -.-> lab-117403{{"`Create ArrayList From Array`"}} java/comments -.-> lab-117403{{"`Create ArrayList From Array`"}} java/data_types -.-> lab-117403{{"`Create ArrayList From Array`"}} java/for_loop -.-> lab-117403{{"`Create ArrayList From Array`"}} java/operators -.-> lab-117403{{"`Create ArrayList From Array`"}} java/output -.-> lab-117403{{"`Create ArrayList From Array`"}} java/strings -.-> lab-117403{{"`Create ArrayList From Array`"}} java/variables -.-> lab-117403{{"`Create ArrayList From Array`"}} java/system_methods -.-> lab-117403{{"`Create ArrayList From Array`"}} end

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