Convert Array to ArrayList

JavaJavaBeginner
Practice Now

Introduction

In Java, an array is used to store similar types of elements, while an ArrayList is an implementation class of the List interface used to store elements index-based. There may be instances when you need to convert an array to an ArrayList when working with collections of data. This lab will teach you how to convert an array to ArrayList in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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/ObjectOrientedandAdvancedConceptsGroup -.-> java/generics("`Generics`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/arraylist("`ArrayList`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/generics -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/arraylist -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/oop -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/identifier -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/arrays -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/data_types -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/operators -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/output -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/strings -.-> lab-117388{{"`Convert Array to ArrayList`"}} java/system_methods -.-> lab-117388{{"`Convert Array to ArrayList`"}} end

Create an array of elements

In this step, create an array of elements, for example, a String array.

String[] fruits = {"Apple", "Orange", "Banana"};

Convert using asList() Method

The asList() method of the Arrays class can be used to convert an array to ArrayList. This method returns a list which we can pass to the ArrayList constructor to convert the list to an ArrayList.

ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(fruits));

Here, arrayList will have all the elements of the fruits array.

Print the converted ArrayList

Using the println() method, you can print the newly-converted ArrayList.

System.out.println(arrayList);

The complete code for steps 2-4 looks like this:

String[] fruits = {"Apple", "Orange", "Banana"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(fruits));
System.out.println(arrayList);

To execute this code, compile the ArrayToArrayList.java file using the following command:

javac ArrayToArrayList.java

Then, run the file using the following command:

java ArrayToArrayList

Convert using addAll() Method

In Java, the addAll() method of the Collections class can be used to add all the elements of an array to an ArrayList. This method is used here instead of asList().

ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList, fruits);

Here, arrayList will also contain all the elements of the fruits array.

Print the converted ArrayList

Using the println() method, you can print the newly-converted ArrayList.

System.out.println(arrayList);

The complete code for steps 5-6 looks like this:

ArrayList<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList, fruits);
System.out.println(arrayList);

To execute this code, compile the ArrayToArrayList.java file using the following command:

javac ArrayToArrayList.java

Then, run the file using the following command:

java ArrayToArrayList

Summary

In this lab, you learned how to convert an array to ArrayList in Java using the asList() and addAll() methods. With this skill, you can easily convert arrays to ArrayLists when required in your Java projects.

Other Java Tutorials you may like