Site icon JavaGoal

How to Convert list to array in java

Before move further we should read what is Array and What is ArrayList in Java?
There may be many situations when we need to Convert list to array in java. There are many methods that allow us to convert ArrayList to array. In this post we will discuss how we convert ArrayList to array by use of different methods.

1. toArray() method
2. toArray(T[] a)
3. get() method

Object[] toArray() method

The toArray() method is used to convert an ArrayList to Array in java. It returns an array that contains all the elements of the ArrayList. It retains the same order of elements as in the ArrayList. Let’s see how to convert list to array in java

public Object[] toArray()

accessModifier, This method is public so it can be accessed from any class.
return type, It returns an array of Object, Here Object is the class that is the topmost class in Java.
parameter, It doesn’t take any parameter.

import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String arg[])
    {
        ArrayList<String> listOfNames = new ArrayList<String>();
        listOfNames.add("JAVA");
        listOfNames.add("GOAL");
        listOfNames.add("LEARNING");
        listOfNames.add("WEBSITE");
        
        ArrayList<Integer> listOfNumber = new ArrayList<Integer>();
        listOfNumber.add(123);
        listOfNumber.add(456);
        listOfNumber.add(789);
        
        // It returning array of Object type
        Object[] namesArray =  listOfNames.toArray();
        System.out.println("String Array:");
        for(Object obj : namesArray)
        	System.out.println(obj);
        
        // It returning array of Object type
        Object[] numberArray =  listOfNumber.toArray();
        System.out.println("Integer Array:");
        for(Object obj : numberArray)
        	System.out.println(obj);
    }
}

Output: String Array:
JAVA
GOAL
LEARNING
WEBSITE
Integer Array:
123
456
789

Note: The toArray() method returns an array of type Object that is Object[].Here Object is topmost class so we need to typecast it. If we do not typecast, the compiler throws the compilation error. Let’s see the example of how it will work?

import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String arg[])
    {
        ArrayList<String> listOfNames = new ArrayList<String>();
        listOfNames.add("JAVA");
        listOfNames.add("GOAL");
        listOfNames.add("LEARNING");
        listOfNames.add("WEBSITE");
        
        ArrayList<Integer> listOfNumber = new ArrayList<Integer>();
        listOfNumber.add(123);
        listOfNumber.add(456);
        listOfNumber.add(789);
        
        // It returning array of Object type
        String[] namesArray =  (String[]) listOfNames.toArray();
        System.out.println("String Array:");
        for(String obj : namesArray)
        	System.out.println(obj);
        
        // It returning array of Object type
        Integer[] numberArray =  (Integer[]) listOfNumber.toArray();
        System.out.println("Integer Array:");
        for(Integer obj : numberArray)
        	System.out.println(obj);
    }
}

Output: Exception in thread “main” java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader ‘bootstrap’) at ArrayListExample.main(ArrayListExample.java:18)

A child class object can’t hold the object of Parent class. Here Object is parent class of all classes so we can’t convert it into Integer or String. To resolve this problem we can use toArray(T[] a) method.

toArray(T[] a) method

The toArray(T[] a) method is also used to convert an ArrayList to Array in java. This method takes a parameter of Array. If ​the size of the specified array is greater than or equal to the size of the ArrayList, then the toArray(T[] a) method fills the elements of the ArrayList in array. Otherwise, it creates a new array and filled up.

public T[] toArray(T[] a)

accessModifier, This method is public so it can be accessed from any class.
return type, it returns an array of T type. Where T is the object of Arrays which programmer wants to return.
parameter, It takes an object Array that programmer wants to fill
Exception, ArrayStoreException is thrown if any element in the list fails to be​ converted into the specified type.

import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String arg[])
    {
        ArrayList<String> listOfNames = new ArrayList<String>();
        listOfNames.add("JAVA");
        listOfNames.add("GOAL");
        listOfNames.add("LEARNING");
        listOfNames.add("WEBSITE");
        
        ArrayList<Integer> listOfNumber = new ArrayList<Integer>();
        listOfNumber.add(123);
        listOfNumber.add(456);
        listOfNumber.add(789);
        
        // Creating blank array with ArrayList size
        String[] namesArray = new String[listOfNames.size()];
        // It returning array of Object type
        namesArray =  listOfNames.toArray(namesArray);
        System.out.println("String Array: ");
        for(String s : namesArray)
        	System.out.println(s);
        
        // Creating blank array with ArrayList size
        Integer[] numbersArray = new Integer[listOfNumber.size()];
        // It returning array of Object type
        numbersArray =  listOfNumber.toArray(numbersArray);
        System.out.println("Integer Array: ");
        for(Integer num : numbersArray)
        	System.out.println(num);
    }
}

Output: String Array:
JAVA
GOAL
LEARNING
WEBSITE
Integer Array:
123
456
789

get(int index) method

If you don’t want to use the toArray() method, you can use get() method and convert the ArrayList to Array.

import java.util.ArrayList;
public class ArrayListExample 
{
    public static void main(String arg[])
    {
        ArrayList<String> listOfNames = new ArrayList<String>();
        listOfNames.add("JAVA");
        listOfNames.add("GOAL");
        listOfNames.add("LEARNING");
        listOfNames.add("WEBSITE");
        
        ArrayList<Integer> listOfNumber = new ArrayList<Integer>();
        listOfNumber.add(123);
        listOfNumber.add(456);
        listOfNumber.add(789);
        
        // Creating blank array with ArrayList size
        String[] namesArray = new String[listOfNames.size()];
        for (int i = 0; i < listOfNames.size(); i++) 
        	namesArray[i] = listOfNames.get(i); 
        
        System.out.println("String Array: ");
        for(String s : namesArray)
        	System.out.println(s);    
    }
}

Output: String Array:
JAVA
GOAL
LEARNING
WEBSITE

Exit mobile version