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"}
- Using ArrayList.get() Method
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.
- Print String Array.
Below is the implementation of the above approach:
// 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));
}
}
Output:ArrayList: [Geeks, for, Geeks] String Array[]: [Geeks, for, Geeks]
- Using Object array as intermediate
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 desired type using typecasting. Here it is converted to String type and added to the String Array.
- Print String Array.
Below is the implementation of the above approach:
// 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));
}
}
Output:ArrayList: [Geeks, for, Geeks] String Array[]: [Geeks, for, Geeks]
- Using copyOf() method:
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.
Below is the implementation of the above approach:
// 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));
}
}
Output:ArrayList: [Geeks, for, Geeks] String Array[]: [Geeks, for, Geeks]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.