Java Convert ArrayList to Array

Tags: ArrayList Array

In this Java core tutorial we learn how to convert an java.util.ArrayList object to an array in Java programming language.

Table of contents

  1. Convert ArrayList to Array of Objects in Java
  2. Convert ArrayList to Array of Specified Type in Java

Convert ArrayList to Array of Objects in Java

In Java, with a given ArrayList object we can use the List.toArray() method to convert it to an Object[] array as the following example Java code.

ConvertArrayListToArrayExample1.java

import java.util.ArrayList;
import java.util.List;

public class ConvertArrayListToArrayExample1 {
    public static void main(String... args) {
        List<String> arrayList = new ArrayList<>();

        arrayList.add("Java");
        arrayList.add("Kotlin");
        arrayList.add("Groovy");

        Object[] array = arrayList.toArray();
        for (Object item : array) {
            System.out.println(item);
        }
    }
}
The output as below.
Java
Kotlin
Groovy

Convert ArrayList to Array of Specified Type in Java

We also can convert an ArrayList to array at specified data type using the List.toArray(T[] a) method as the following example Java code.

ConvertArrayListToArrayExample2.java

import java.util.ArrayList;
import java.util.List;

public class ConvertArrayListToArrayExample2 {
    public static void main(String... args) {
        List<String> arrayList = new ArrayList<>();

        arrayList.add("Java");
        arrayList.add("Kotlin");
        arrayList.add("Groovy");

        String[] array = new String[arrayList.size()];
        arrayList.toArray(array);
        for (String item : array) {
            System.out.println(item);
        }
    }
}
The output as below.
Java
Kotlin
Groovy

Happy Coding 😊

Convert Array to List in Java

Java Convert ArrayList to String

Java Convert ArrayList to Comma Separated String

Java Convert ArrayList to HashSet

Java Convert ArrayList to LinkedList

Java Create New Array with Class Type and Length

Java Create ArrayList using List Interface

Java Get Index of Minimum Value in ArrayList

How to use Java ArrayList with Examples

Java Convert Array to ArrayList

Java Insert Element to ArrayList at Specified Index

Java Get Minimum Value in ArrayList

How to Traverse ArrayList using Iterator in Java

How to Traverse ArrayList using for each loop in Java

How to Traverse ArrayList using forEach() method in Java

Java Add Element to ArrayList