CodeGym /Java Blog /Java Collections /Convert List to Array in Java
Author
Pavlo Plynko
Java Developer at CodeGym

Convert List to Array in Java

Published in the Java Collections group
List and arrays are two ways to store data in Java that you will use quite often. In projects where you need to store data without any sort of file handling, list and arrays let you store data throughout the execution of your program. We’ll be using Class ArrayList that implements List Interface in Java interchangeably throughout this tutorial.

Difference Between List and Array

ListArray
The size of a list can be changedThe size of an array can’t be changed
You can’t store Primitive types in ListYou can store Primitive types in Array
You can use Generics with ListYou can’t use Generics with Array
Consumes more memoryConsumes less memory

Converting List to Array with Library Function

List has a toArray() method which directly converts the contents of any list into an array while retaining the placement of text in the Array as it was in the original list. Here is the algorithm/steps to convert a list to array in java using this built-in library function.
  1. Initialize an ArrayList.
  2. Add elements to the List through the list.add(data_type) method.
  3. Create an Array with the same size as the list.
  4. Convert the List into an Array by using the variable name of the array created in step 3 as an argument.
  5. Print the contents of the Array.
The code implementing these steps is given below.

import java.util.ArrayList;
public class convertListToArray {
 
    public static void main(String[] args) {
 
        //Converting List to Array With Library Function
 
        //Declaration of Array List
        ArrayList<String> sampleList = new ArrayList<String>();
 
        //Adding Elements to Array List
        sampleList.add("California");
        sampleList.add("Texas");
        sampleList.add("Illinois");
        sampleList.add("Massachusetts");
        sampleList.add("Florida");
        sampleList.add("Virginia");
        sampleList.add("Colorado");
 
 
        //Printing the Array List
        System.out.println("Elements of List: " + sampleList);
 
        //Declaring Array with Equal Size to the List
        String[]arr = new String [sampleList.size()];
 
        //Converting List to Array
        sampleList.toArray(arr);
 
        //Printing the Array
        System.out.print("Elements of Array: ");
        for (int i = 0 ; i < arr.length ; i++){
            System.out.print(arr[i] + "  ");
        }
    }
}

Converting List to Array Without Library Function

Sometimes, there may be a requirement to convert the list into an array without using built-in functions. The problem at hand may use certain restrictions in terms of library functions. This requirement is common for programming students that are encouraged to work without library functions to help them learn the inner workings of these functions before implementing them. In such cases, you can use the following steps to convert a list into an array in java without any library function.
  1. Initialize an ArrayList.
  2. Add elements to the List through the list.add(data_type) method.
  3. Create an Array with the same size as the list.
  4. Create a for loop that will iterate through each element of the ArrayList and pass it through to the Array[index] through the list.get(index) function.
  5. Print the contents of the Array to show you’re actually converting the list to an array.
The code for implementing these steps is given below.

import java.util.ArrayList;
public class converListToArray2 {
 
    public static void main(String[] args) {
        //Converting List to Array Without Library Functions
 
        //Declaration of Array List
        ArrayList<String> sampleList = new ArrayList<String>();
 
        //Adding Elements to Array List
        sampleList.add("California");
        sampleList.add("Texas");
        sampleList.add("Illinois");
        sampleList.add("Massachusetts");
        sampleList.add("Florida");
        sampleList.add("Virginia");
        sampleList.add("Colorado");
 
 
        //Printing the Array List
        System.out.println("Elements of List: " + sampleList);
 
        //Declaring Array with Equal Size to the List
        String[]arr = new String [sampleList.size()];
 
        //Converting to Array
        for (int i = 0 ; i < arr.length ; i++){
            arr[i] = sampleList.get(i);
        }
 
        //Printing the Array
        System.out.print("Elements of Array: ");
        for (int i = 0 ; i < arr.length ; i++){
            System.out.print(arr[i] + "  ");
        }
    }
}
Similarly, if you’re also wondering how to convert an Array into a List, you can do so by using the built-in Array.asList() method or iterating through the array and storing values at each index in the List.
Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jeongmin Level 8, Tsche-mul-p-ho, Korea, Republic of
12 September 2022
In a code "ArrayList<string> sampleList = new ArrayList<string>();", two "string" needs to be changed as "String"