How do I convert Array to List (ArrayList) in Java

Published April 30, 2022

Although arrays are simple and easy to use, they have many limitations, such as a fixed size that makes it difficult to add a new element at the beginning and rearrange elements. Thanks to the Collections Framework, we can implement lists, sets, and queues in many useful ways. We have access to numerous useful implementations of lists, sets, and queues in the Collections Framework. One of these is an array list, which is versatile, widely used, and has a great deal of versatility.

While converting an array to Java, there are three methods that you can use. These methods include:

a. The Naïve or Brute Force Method

b. The Arrays.asList() Method

c. The Collections.addAll() Method

 

a. Using the Naïve or Brute Force Method

In this method, a List is created from an empty Array, and each element in the array is added to it one by one. This method works following the steps below:

·         Step 1: Get an Array

·         Step 2: Create an empty list

·         Step 3: Iterate through the items in the array

·         Step 4: Now, add each item in the array

·         Step 5: Return a complete list.

 

Example

This example converts an array of fruits into a list of fruits

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class NaivemethodExample {

    public static <T> List<T> convertArrayToList(T array[])

    {

        List<T> list = new ArrayList<>();

        for (T t : array) {

            list.add(t);

        }

        return list;

    }

    public static void main(String args[])

    {

        String array[]

            = { "Mangoes", "Oranages", "berries" };

        System.out.println("Array: "

                           + Arrays.toString(array));

        List<String> list = convertArrayToList(array);

        System.out.println("List: " + list);

    }

}

 

b.The Arrays.asList() Method

With the help of the arrays as.List() method, the array is passed into the List constructor as the parameter for the constructor.

This method performs the steps below to convert an array into a list:

  • Step 1: Gets the array

  • Step 2:  Creates a Listbypassing the Array as a parameter in the constructor of the list.

  • Step 3: Return a complete list.

 

Example

This example uses  the Arrays.asList() method to convert an array of fruits into a List

 

package asList;

import java.util.Arrays;

import java.util.List;

public class asListExample {

    public static <T> List<T> convertArrayToList(T array[])

    {

        List<T> list = Arrays.asList(array);

        return list;

    }

    public static void main(String args[])

    {

        String array[]

            = { "Mangoes", "Oranges", "berries" };

        System.out.println("Array: "

                          + Arrays.toString(array));

        List<String> list = convertArrayToList(array);

        System.out.println("List: " + list);

    }

}

 

c.The Collections.addAll() Method

Since Lists are part of the Collection package in Java, it's possible to convert an Array into a List by using the Collections.addAll() method.

This method converts an array into a list using the following basic steps:

  • Step 1:  gets the Array

  • Step 2: Creation of an empty list

  • Step 3:  converts array into the list using the collections.addAll() method

  • Step 4: Finally, returns a list

Example

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class collectionsall {

    public static <T> List<T> convertArrayToList(T array[])

    {

        List<T> list = new ArrayList<>();

        Collections.addAll(list, array);

        return list;

    }

 

    public static void main(String args[])

    {

        String array[]

            = { "peas", "tomatoes", "water melons" };

        System.out.println("Array: "

                           + Arrays.toString(array));

        List<String> list = convertArrayToList(array);

        System.out.println("List: " + list);

    }

}

 

Output:

Array: [peas, tomatoes, water melons ]

List: [peas, tomatoes, water melons ]

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

124 Views