Java ArrayList to String Array Example

The following sample code shows how to convert a Java ArrayList of Strings to a standard Java String Array using the ArrayList.toArray() method. This method will return an array containing all of the elements in this list in proper sequence, which is first element to the last element.


Java ArrayList to String Array

package com.jcd.java.tutorials;

import java.util.ArrayList;

public class JavaListToArrayString 
{

	public static void main(String[] args) 
	{
		
		//Convert ArrayList to String Array
		ArrayList<String> jcdStringList = new ArrayList<String>();
		jcdStringList.add("Java");
		jcdStringList.add("Code");
		jcdStringList.add("Depot");
		jcdStringList.add("Com");
		
		String[] jcdStringArray = new String[jcdStringList.size()];
		jcdStringArray = jcdStringList.toArray(jcdStringArray);
		
		for(String jcdStr : jcdStringArray) 
		{
			System.out.println(jcdStr);
		}

	}

}

Java ArrayList to String Array Sample Output

Java
Code
Depot
Com



Java References:

Add a Comment

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