In this post, we will see how to convert list of string to array of string in Java.
1. List.toArray()
We can use toArray(T[])
method to copy the list into a newly allocated string array. We can either pass a string array as an argument to toArray()
method or pass an empty array of String type. If an empty array is passed, JVM will allocate memory for string array. Please note that if no argument is passed to toArray()
, it will return an Object
array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class ListUtil { // convert list of string to array of string in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = list.toArray(new String[0]); System.out.println(Arrays.toString(array)); } } |
Output:
[NYC, New Delhi]
2. Java 8
Java 8 provides another simple approach to convert list of string to array of string. Below are the steps:
- Convert the specified list of string to a sequential Stream.
- Use
toArray()
method to accumulate elements of the stream into a new string array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class ListUtil { // convert list of string to array of string in Java 8 public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = list.stream() .toArray(String[]::new); System.out.println(Arrays.toString(array)); } } |
Output:
[NYC, New Delhi]
3. Arrays.copyOf()
We can also use Arrays.copyOf() to copy the specified array to an array of specified type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class ListUtil { // convert list of string to array of string in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = Arrays.copyOf(list.toArray(), list.size(), String[].class); System.out.println(Arrays.toString(array)); } } |
Output:
[NYC, New Delhi]
4. System.arraycopy()
We can use System.arraycopy() that copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.List; class ListUtil { // convert list of string to array of string in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); String[] array = new String[list.size()]; System.arraycopy(list.toArray(), 0, array, 0, list.size()); System.out.println(Arrays.toString(array)); } } |
Output:
[NYC, New Delhi]
5. Naive
This post is incomplete without re-inventing the wheels. Naive approach is to use regular for loop to iterate over the list of string and simply copy elements from list to string array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; class ListUtil { // convert list of string to array of string in Java public static void main(String[] args) { List<String> list = Arrays.asList("NYC", "New Delhi"); // allocate memory for string array String[] array = new String[list.size()]; // copy elements from list to string array for (int i = 0; i < array.length; i++) array[i] = list.get(i); System.out.println(Arrays.toString(array)); } } |
Output:
[NYC, New Delhi]
Thanks for reading.
Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply