Convert an ArrayList of String to a String Array in Java
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
import java.util.ArrayList;
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
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);
}
for (String k : str) {
System.out.println(k);
}
}
}
|
Output
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add( "Anshul Aggarwal" );
al.add( "Mayank Solanki" );
al.add( "Abhishek Kelenia" );
al.add( "Vivek Gupta" );
String k[] = al.toArray( new String[al.size()]);
for (String str : k) {
System.out.println(str);
}
}
}
|
Output
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
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));
}
}
|
Output
[Anshul Aggarwal, Mayank Solanki, Abhishek Kelenia, Vivek Gupta]
Last Updated :
03 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...