Convert an ArrayList of String to a String Array in Java - GeeksforGeeks

03 Dec, 2021
In Java, as we all know ArrayList class is derived from the List interface. Here we are given an ArrayList of strings and the task is to convert the ArrayList to a string array.
Illustration:
Input : ArrayList = [ "Geeks", "for", "Geeks" ]
Output: String[] = {"Geeks", "for", "Geeks"}
Input : ArrayList = [ "Jupiter", "Saturn", "Uranus", "Neptune", "Sun"]
Output: String[] = {"Jupiter", "Saturn", "Uranus", "Neptune", "Sun"}
Methods:
Using get() method of ArrayList classUsing toArray() method of ArrayList classUsing copyOf() method of Arrays class
Using get() method of ArrayList class
Using toArray() method of ArrayList class
Using copyOf() method of Arrays class
Syntax: 
str[j] = a1.get(j)
Approach: 
Get the ArrayList of Strings.Find the size of ArrayList using size() method, and Create a String Array of this size.Fetch each element of the ArrayList one by one using get() method.Assigned each element into String Array using assignment(=) operator.Printing string array.
Get the ArrayList of Strings.
Find the size of ArrayList using size() method, and Create a String Array of this size.
Fetch each element of the ArrayList one by one using get() method.
Assigned each element into String Array using assignment(=) operator.
Printing string array.
Example:
Java
// Java Program to Convert ArrayList to Array// using toArray() Method // Importing required classesimport java.util.ArrayList;import java.util.Arrays; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Creating an empty ArrayList of string type        ArrayList<String> al = new ArrayList<String>();         // Populating the ArrayList by custom elements        al.add("Anshul Aggarwal");        al.add("Mayank Solanki");        al.add("Abhishek Kelenia");        al.add("Vivek Gupta");         String[] str = new String[al.size()];         for (int i = 0; i < al.size(); i++) {            str[i] = al.get(i);        }         // Printing using for each loop        for (String k : str) {            System.out.println(k);        }    }}
Anshul Aggarwal
Mayank Solanki
Abhishek Kelenia
Vivek Gupta
Here we will be creating an object array to store elements received from ArrayList by creating an array of strings.
Syntax: 
Object[] arr = a1.toArray()
String str = (String)obj;
Approach: 
Get the ArrayList of String.Convert ArrayList to Object array using toArray() method.Iterate and convert each element to the desired type using typecasting. Here it is converted to String type and added to the string array.Print the string array
Get the ArrayList of String.
Convert ArrayList to Object array using toArray() method.
Iterate and convert each element to the desired type using typecasting. Here it is converted to String type and added to the string array.
Print the string array
Example:
Java
// Java Program to Convert ArrayList to Array// using toArray() Method // Importing required classesimport java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Creating an empty ArrayList of string type        ArrayList<String> al = new ArrayList<String>();         // Populating the ArrayList by custom elements        al.add("Anshul Aggarwal");        al.add("Mayank Solanki");        al.add("Abhishek Kelenia");        al.add("Vivek Gupta");         // Converting above List to array using toArray()        // method and storing it in an string array        String k[] = al.toArray(new String[al.size()]);         // Iterating over above string array        for (String str : k) {             // Printing the elements in above array            System.out.println(str);        }    }}
Anshul Aggarwal
Mayank Solanki
Abhishek Kelenia
Vivek Gupta
Syntax: 
Object[] gfg = a1.toArray()
String[] str = Arrays.copyOf(gfg, gfg.length, String[].class);
 Approach: 
Get the ArrayList of String.Convert ArrayList to Object array using toArray() method.Convert it to String Array using Arrays.copyOf() method.Print String Array.
Get the ArrayList of String.
Convert ArrayList to Object array using toArray() method.
Convert it to String Array using Arrays.copyOf() method.
Print String Array.
Example:
Java
// Java Program to Convert ArrayList to Array// using toArray() Method // Importing required classesimport java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Creating an empty ArrayList of string type        ArrayList<String> al = new ArrayList<String>();         // Populating the ArrayList by custom elements        al.add("Anshul Aggarwal");        al.add("Mayank Solanki");        al.add("Abhishek Kelenia");        al.add("Vivek Gupta");         String[] answer = Arrays.copyOf(            al.toArray(), al.size(), String[].class);        System.out.println(Arrays.toString(answer));    }}
[Anshul Aggarwal, Mayank Solanki, Abhishek Kelenia, Vivek Gupta]
solankimayank
surindertarika1234
adnanirshad158
Java-Array-Programs
Java-ArrayList
Java-Collections
Java-List-Programs
Java-String-Programs
Technical Scripter 2018
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
Stream In Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java