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 class
- Using toArray() method of ArrayList class
- Using copyOf() method of Arrays class
Method 1: Using ArrayList.get() Method of ArrayList 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.
Example:
Java
// Java Program to Convert ArrayList to Array // using toArray() Method Â
// Importing required classes import java.util.ArrayList; import java.util.Arrays; Â
// Main class class 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
Method 2: Using toArray() method of ArrayList classÂ
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
Example:
Java
// Java Program to Convert ArrayList to Array // using toArray() Method Â
// Importing required classes import java.util.*; Â
// Main class class 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
Method 3: Using copyOf() method of Arrays classÂ
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.
Example:
Java
// Java Program to Convert ArrayList to Array // using toArray() Method Â
// Importing required classes import java.util.*; Â
// Main class class 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]