Java Program to convert String ArrayList to String Array

Hello coders, welcome to codingbroz ! Today you are going to learn How to convert String ArrayList to String Array in Java ?

Array to ArrayList or ArrayList to Array conversion in Java is a very common programming task which often occurs while any Java coder are solving any Java programs related to Arrays or ArrayLists.

Being a Java problem you can often get to deal with the task of converting ArrayList to Array and vice-versa. So, we are going to see How to convert String ArrayList to String Arrays in Java ?

In Java, ArrayLists are backed with Array data structures and arrays in Java are Objects, like Strings which are also Objects in Java. So, in this article we are first going to create an ArrayList which stores Strings and then to convert it from ArrayList to array, we can use the toArray() method.

How to convert String ArrayList to Array in Java

Now, Let’s see full code example of Java to convert ArrayList of String to array of String in Java. In this Java Program we first create an ArrayList to store some Strings and will then convert the String ArrayList to Array of String.

Java program to convert String ArrayList to String Array :

import java.util.*;

public class ArrayListToArray{
        public static void main (String[] args) throws java.lang.Exception
	{
	    // Initializing a String ArrayList
		ArrayList<String> list = new ArrayList<>();
		// Storing String in the ArrayList
		list.add("Hello");
		list.add("coders");
		list.add("Welcome to");
		list.add("www.codingbroz.com");
		
		Object objectArray[] = list.toArray();
		String strArr[] = Arrays.copyOf(objectArray,objectArray.length,String[].class);
		for(int i=0 ; i<strArr.length ;i++){
		    System.out.println(strArr[i]);
		}
		
	}
}
Output : 

Hello 
coders
Welcome to
www.codingbroz.com

So, this is How to convert String ArrayList to String Array in Java? Using this method you can now convert Interger ArrayList or Double ArrayList to arrays in Java. Thanks for learning with CodingBroz !!!

Leave a Comment

Your email address will not be published.