How to make ArrayList elements to an Array?

Here simply use toArray method. toArray method return an Array.First of all declare a ArrayList type of String. Add five string type elements. Declare an String Array which length is equal to ArrayList size.


								
package com.tech.mindclues;

import java.util.ArrayList;

public class ArrayListConvertToArray {

	public static void main(String args[]) {
		
		ArrayList listToArray = new ArrayList();
		
		listToArray.add("Mind");
		listToArray.add("Clues");
		listToArray.add("Hunt");
		listToArray.add("Hagrid");
		listToArray.add("Tom");
		
		//declare a array which length is equal to arraylist size.
		String[] ary = new String[listToArray.size()];
		
		ary = listToArray.toArray(ary);//Using toArray method to set the ArrayList to Array
		
		System.out.println("Array elemens  :");
		
		for (int i = 0; i < ary.length; i++) {
			
			System.out.println(ary[i]);
		}

	}

}

							

								
Array elemens  :
Mind
Clues
Hunt
Hagrid
Tom

							

Related Articles

post a comment