Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How might I convert an ArrayList<String> object to a String[] array in Java?

share|improve this question
up vote 1033 down vote accepted
List<String> list = ..;
String[] array = list.toArray(new String[0]);

For example:

List<String> list = new ArrayList<String>();
//add some stuff
list.add("android");
list.add("apple");
String[] stringArray = list.toArray(new String[0]);

The toArray() method without passing any argument returns Object[]. So you have to pass an array as an argument, which will be filled with the data from the list, and returned. You can pass an empty array as well, but you can also pass an array with the desired size.

Important update: Originally the code above used new String[list.size()]. However, this blogpost reveals that due to JVM optimizations, using new String[0] is better now.

share|improve this answer
10  
Does the size of the argument make any difference? – Thorbjørn Ravn Andersen May 15 '12 at 13:09
26  
it saves one more array instnatiation – Bozho May 15 '12 at 13:28
    
It shows this warning in logcat: Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f050009} – Adil Malik Jun 19 '13 at 12:40
15  
Turns out that providing a zero-length array, even creating it and throwing it away, is on average faster than allocating an array of the right size. For benchmarks and explanation see here: shipilev.net/blog/2016/arrays-wisdom-ancients – Stuart Marks Jan 21 at 8:29
2  
@StuartMarks exactly, I even have a note to update my answer after I read that post two weeks ago :) doing it now – Bozho Feb 16 at 14:36

An alternative in Java 8:

String[] strings = list.stream().toArray(String[]::new);
share|improve this answer
20  
Is there any performance benefits of using this approach ? – aProgrammer May 28 '15 at 6:49
4  
Or any benefits? – James Watkins Apr 2 at 13:50
1  
I would prefer that syntax, but IntelliJ displays a compiler error with that, complains "T[] is not a functional interface." – Glen Mazza Jul 25 at 16:11
    
@GlenMazza you can only use toArray on a Stream object. This compilation error may occur if you reduce the stream using a Collectors and then try to apply toArray. – Udara Bentota Nov 16 at 7:29
ArrayList<String> arrayList = new ArrayList<String>();
Object[] objectList = arrayList.toArray();
String[] stringArray =  Arrays.copyOf(objectList,objectList.length,String[].class);

Using copyOf, ArrayList to arrays might be done also.

share|improve this answer
2  
Suggest camelcase so "objectList =..." and "stringArray". Also, it is Arrays.copyOf...capital O. – Jason Weden Aug 28 '14 at 15:18

You can use the toArray() method for List:

ArrayList<String> list = new ArrayList<String>();

list.add("apple");
list.add("banana");

String[] array = list.toArray(new String[list.size()]);

Or you can manually add the elements to an array:

ArrayList<String> list = new ArrayList<String>();

list.add("apple");
list.add("banana");

String[] array = new String[list.size()];

for (int i = 0; i < list.size(); i++) {
    array[i] = list.get(i);
}

Hope this helps!

share|improve this answer

In Java 8:

String[] strings = list.parallelStream().toArray(String[]::new);
share|improve this answer
4  
This is really already contained in this answer. Perhaps add it as an edit to that answer instead of as an entirely new one. – River Feb 5 at 16:17
List <String> list = ...
String[] array = new String[list.size()];
int i=0;
for(String s: list){
  array[i++] = s;
}
share|improve this answer
12  
This works, but isn't super efficient, and duplicates functionality in the accepted answer with extra code. – Alan Delimon Feb 8 '13 at 15:26

By using toArray() method of ArrayList you can get 0bject[]. Cast that Object[] to String[] Here the sample code:

ArrayList<String> arr_List=new ArrayList<String>();
Object[] str_Arr=arr_List.toArray();
share|improve this answer
11  
Object[]can not be cast to String[]. – Volker Seibt Apr 9 '15 at 15:55
1  
As @VolkerSeibt said,you can't cast an object array to string array,it will results in a java.lang.ClassCastException – ShihabSoft Dec 2 '15 at 11:05

This is enough:

int[] d = new int[list.size()];

for (int i = 0; i < list.size(); i++) {
    d[i] = list.get(i);
}
share|improve this answer
8  
Except that we have Strings, not ints. – Teepeemm Sep 18 '13 at 21:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.