Write a java program to convert arraylist to array in java?

This tutorial explains how to convert arraylist to array in java.We use predefined method toArray() in list class in order to convert arraylist to array in java.

Here are the signatures of toArray() method :

public Object[] toArray()
public <T> T[]toArray(T[] a)

 

Java example to convert arraylist to array in java

import java.util.List;

public class ArrayListToArray {

	public static void main(String[] args) {

		List<Integer> list=new ArrayList<>();
		list.add(1);
		list.add(2);
		list.add(3);
		Integer []intArray=new Integer[list.size()];
		list.toArray(intArray);
		for (Integer integer : intArray) {
			System.out.println(integer);
		}

	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *