Convert an ArrayList of String to a String array in Java


Given an ArrayList of String, the task is to convert the ArrayList to String array in java.

Examples:

Input: ArrayList = [ "Geeks", "for", "Geeks" ]
Output: String[] = {"Geeks", "for", "Geeks"}

Input: ArrayList = [ "Jupiter", "Saturn", "Uranus", "Neptune", "Sun"]
Output: String[] = {"Jupiter", "Saturn", "Uranus", "Neptune", "Sun"}
  1. Using ArrayList.get() Method

    Syntax:

     str[j] = a1.get(j)

    Approach:

    1. Get the ArrayList of Strings.
    2. Find the size of ArrayList using size() method, and Create a String Array of this size.
    3. Fetch each element of the ArrayList one by one using get() method.
    4. Assigned each element into String Array using assignment(=) operator.
    5. Print String Array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to Convert ArrayList to
    // String ArrayList using get() method
      
    import java.util.*;
      
    public class GFG {
      
        // Function to convert ArrayList<String> to String[]
        public static String[] GetStringArray(ArrayList<String> arr)
        {
      
            // declaration and initialise String Array
            String str[] = new String[arr.size()];
      
            // ArrayList to Array Conversion
            for (int j = 0; j < arr.size(); j++) {
      
                // Assign each value to String array
                str[j] = arr.get(j);
            }
      
            return str;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // declaration and initialise ArrayList
            ArrayList<String>
                a1 = new ArrayList<String>();
      
            a1.add("Geeks");
            a1.add("for");
            a1.add("Geeks");
      
            // print ArrayList
            System.out.println("ArrayList: " + a1);
      
            // Get String Array
            String[] str = GetStringArray(a1);
      
            // Print Array elements
            System.out.print("String Array[]: "
                             + Arrays.toString(str));
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [Geeks, for, Geeks]
    String Array[]: [Geeks, for, Geeks]
    
  2. Using Object array as intermediate

    Syntax:

    Object[] arr = a1.toArray()
    String str = (String)obj;
    

    Approach:

    1. Get the ArrayList of String.
    2. Convert ArrayList to Object array using toArray() method.
    3. Iterate and convert each element to desired type using typecasting. Here it is converted to String type and added to the String Array.
    4. Print String Array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to Convert ArrayList to
    // String ArrayList using get() method
      
    import java.util.*;
      
    public class GFG {
      
        // Function to convert ArrayList<String> to String[]
        public static String[] GetStringArray(ArrayList<String> arr)
        {
      
            // declaration and initialise String Array
            String str[] = new String[arr.size()];
      
            // Convert ArrayList to object array
            Object[] objArr = arr.toArray();
      
            // Iterating and converting to String
            int i = 0;
            for (Object obj : objArr) {
                str[i++] = (String)obj;
            }
      
            return str;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // declaration and initialise ArrayList
            ArrayList<String>
                a1 = new ArrayList<String>();
      
            a1.add("Geeks");
            a1.add("for");
            a1.add("Geeks");
      
            // print ArrayList
            System.out.println("ArrayList: " + a1);
      
            // Get String Array
            String[] str = GetStringArray(a1);
      
            // Print Array elements
            System.out.print("String Array[]: "
                             + Arrays.toString(str));
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [Geeks, for, Geeks]
    String Array[]: [Geeks, for, Geeks]
    
  3. Using copyOf() method:

    Syntax:

    Object[] gfg= a1.toArray()
    String[] str = Arrays.copyOf(gfg, 
                                 gfg.length, 
                                 String[].class);
    

    Approach:

    1. Get the ArrayList of String.
    2. Convert ArrayList to Object array using toArray() method.
    3. Convert it to String Array using Arrays.copyOf() method.
    4. Print String Array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to Convert ArrayList to
    // String ArrayList using get() method
      
    import java.util.*;
      
    public class GFG {
      
        // Function to convert ArrayList<String> to String[]
        public static String[] GetStringArray(ArrayList<String> arr)
        {
      
            // Convert ArrayList to object array
            Object[] objArr = arr.toArray();
      
            // convert Object array to String array
            String[] str = Arrays
                               .copyOf(objArr, objArr
                                                   .length,
                                       String[].class);
      
            return str;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // declaration and initialise ArrayList
            ArrayList<String>
                a1 = new ArrayList<String>();
      
            a1.add("Geeks");
            a1.add("for");
            a1.add("Geeks");
      
            // print ArrayList
            System.out.println("ArrayList: " + a1);
      
            // Get String Array
            String[] str = GetStringArray(a1);
      
            // Print Array elements
            System.out.print("String Array[]: "
                             + Arrays.toString(str));
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [Geeks, for, Geeks]
    String Array[]: [Geeks, for, Geeks]
    


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.