How to Convert Arraylist to Array in Java

Mostly all Java developers will be familiar with Arraylist and Array, and I believe most of us will come into a situation in your projects to convert Arraylist to array or vice versa. Usually what beginners will do is, they first will create get the size of an arraylist and create one array and loop through the arraylist and push each element into that array. But if any senior developers will review that code, they will not accept those as a best coding practice one. Because in List API itself we have a method to convert arraylist to array. Today we will see How to Convert Arraylist to Array in Java along with some example.

Convert Arraylist to Array in Java

Convert Arraylist to Array in Java

Method to convert Arraylist to Array in Java:

In Arraylist we have a method called toArray which will convert arraylist to array. In toArray method we have to pass array type with the size initialized to the size of arraylist.

Syntax

listName.toArray(new listType[size])

Eg:

studentList.toArray(new Student[studentList.size()]);

Also Read : How to sort User Defined Objects in ArrayList

Now we will see along with some code snippet.

Employee.java

package com.learnfromexamples.model;

public class Employee {
    private String empno;
    private String empName;
    
    public Employee(String empno, String empName ) {
        this.empno = empno;
        this.empName = empName;
    }
    
    public String getEmpno() {
        return empno;
    }
    
    public void setEmpno(String empno) {
        this.empno = empno;
    }
    
    public String getEmpName() {
        return empName;
    }
    
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    @Override
    public String toString() {
        return "Empno is "+ empno + " Empname is " + empName;
    }
}

ArrayListConversion.java

package com.learnfromexamples.collections.conversion;

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

import com.learnfromexamples.model.Employee;

public class ArrrayListConversion {
    public static void main(String[] args) {
        List<Employee> empList = new ArrayList<Employee>();
        empList.add(new Employee("001", "Shidhar"));
        empList.add(new Employee("002", "Pathy"));
        empList.add(new Employee("003", "Sahana"));
        
        Employee[] empArray  = empList.toArray(new Employee[empList.size()]);
        for (Employee employee : empArray) {
            System.out.println(employee);
        }
    }
}

 

 Output


Empno is 001 Empname is Shidhar
Empno is 002 Empname is Pathy
Empno is 003 Empname is Sahana

Note:

In the above mentioned code snippet I have created Employee Class along with overriding the toString() method. So when I print the Employee object it will give meaningful information like empno and empName.
Now the conversion has become very simple rite, so from now onwards we will use toArray method to convert arraylist to array in Java. Similar to this arraylist to array conversion we will see How to Convert Array to ArrayList in our next blog.