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]