Convert Array to List

JavaJavaBeginner
Practice Now

Introduction

In Java, an array is a collection of elements of the same data type, whereas a list is a collection of elements of any data type. In some scenarios, we may need to convert an array to a list in Java. There are three ways to achieve this. In this lab, we will guide you through the steps required to convert an array to a list in Java.


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/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/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") 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/operators("`Operators`") java/StringManipulationGroup -.-> java/strings("`Strings`") subgraph Lab Skills java/scope -.-> lab-117412{{"`Convert Array to List`"}} java/generics -.-> lab-117412{{"`Convert Array to List`"}} java/arraylist -.-> lab-117412{{"`Convert Array to List`"}} java/classes_objects -.-> lab-117412{{"`Convert Array to List`"}} java/class_methods -.-> lab-117412{{"`Convert Array to List`"}} java/modifiers -.-> lab-117412{{"`Convert Array to List`"}} java/oop -.-> lab-117412{{"`Convert Array to List`"}} java/packages_api -.-> lab-117412{{"`Convert Array to List`"}} java/identifier -.-> lab-117412{{"`Convert Array to List`"}} java/arrays -.-> lab-117412{{"`Convert Array to List`"}} java/comments -.-> lab-117412{{"`Convert Array to List`"}} java/data_types -.-> lab-117412{{"`Convert Array to List`"}} java/operators -.-> lab-117412{{"`Convert Array to List`"}} java/strings -.-> lab-117412{{"`Convert Array to List`"}} end

Create a Java file

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

cd ~/project
touch ArrayToList.java

Add import statements and class header

Add the following import statements and class header to the ArrayToList.java file

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

public class ArrayToList {
    public static void main(String[] args) {
        // Code to convert array to list
    }
}

Convert array to list using add() method

In this example, we are using the add() method to add each element of the array to a list explicitly. This method is the most intuitive way to convert an array to a list. Add the following code inside the main() method.

String[] courses = {"Bangalore","Mumbai","Delhi","Noida"};
List<String> courseList = new ArrayList<String>();
for (String course : courses) {
    courseList.add(course);
}

Convert array to list using Arrays.asList() method

In this example, we are using the asList() method from the Arrays class to convert an array to a list. This method is more efficient than the previous method because it doesn't require traversing the entire array. Add the following code inside the main() method.

String[] courses = {"Bangalore","Mumbai","Delhi","Noida"};
List<String> courseList = new ArrayList<String>(Arrays.asList(courses));

Convert array to list using Collections.addAll() method

In this example, we are using the addAll() method from the Collections class to convert an array to a list. This method accepts two parameters, the list and the array, and converts the array to a list. Add the following code inside the main() method.

String[] courses = {"Bangalore","Mumbai","Delhi","Noida"};
List<String> courseList = new ArrayList<String>();
Collections.addAll(courseList, courses);

Compile and run the code

Save the ArrayToList.java file and compile it using the following command:

javac ArrayToList.java

Then, run the code using the following command:

java ArrayToList

View the output

After running the code, you can view the output in the terminal. The output will be the same for all three methods:

Bangalore
Mumbai
Delhi
Noida

Summary

In this lab, we learned how to convert an array to a list in Java. We discussed three methods to achieve this. The first method is the most intuitive but requires traversing the entire array. The other two methods are more efficient and don't require traversing the entire array. We also provided a step-by-step guide to convert an array to a list using each of these methods.

Other Java Tutorials you may like