Java Program to Convert an Array to Array-List

In the previous article, we have seen Java Program to Find the Duplicate Values of an Array of String Values

In this article we are going to see how we can convert an Array into an Arraylist in Java.

Java Program to Convert an Array to Array-List

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways how to convert an Array into an ArrayList.

Method-1: Java Program to Convert an Array to Array-List By Using asList() Function

Approach:

  • Create an array
  • Display the array to the user
  • Convert the array into array list by using Arrays.asList( ) function.
  • Display the arraylist.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Creating the array
        String[] arr = {"Mango", "Apple", "Papaya"};
        // Displaying the array to the user
        System.out.print("Array: ");
        printArray(arr);

        // converting array to arraylist
        ArrayList<String> arrlist= new ArrayList<>(Arrays.asList(arr));
        System.out.println("ArrayList: " + arrlist);
    }

    // Function to print the array
    static void printArray(String arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

Array: Mango Apple Papaya 
ArrayList: [Mango, Apple, Papaya]

Method-2: Java Program to Convert an Array to Array-List By using addAll() Function

Approach:

  • Create an array
  • Display the array to the user
  • Create an array list
  • Convert array to arraylist by using Collections.addAll() function.
  • Display the arraylist.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Creating the array
        String[] arr = {"Mango", "Apple", "Papaya"};
        // Displaying the array to the user
        System.out.print("Array: ");
        printArray(arr);

        // converting array to arraylist
        List<String> arrlist = new ArrayList<String>();
        Collections.addAll(arrlist, arr);
        System.out.println("ArrayList: " + arrlist);
    }

    // Function to print the array
    static void printArray(String arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

Array: Mango Apple Papaya
ArrayList: [Mango, Apple, Papaya]

Method-3: Java Program to Convert an Array to Array-List By Using Manual Method

Approach : 

  • Create an array
  • Display the array to the user
  • Create an array list
  • Copy all the elements from the array into the arraylist using a for loop traversing each array element.
  • Display the arraylist.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Creating the array
        String[] arr = {"Mango", "Apple", "Papaya"};
        // Displaying the array to the user
        System.out.print("Array: ");
        printArray(arr);

        // Creating a new arrylist
        List<String> arrlist = new ArrayList<String>();
        // Copying elements from array into the arraylist individually
        for(String str:arr)
            arrlist.add(str);
        System.out.println("ArrayList: " + arrlist);
    }

    // Function to print the array
    static void printArray(String arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

Array: Mango Apple Papaya 
ArrayList: [Mango, Apple, Papaya]

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: