Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to convert string ArrayList object to String array object in java ?

share|improve this question

3 Answers

up vote 122 down vote accepted
List<String> list = ..;
String[] array = list.toArray(new String[list.size()]);

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.

share|improve this answer
Thank you for your help – Alex Oct 28 '10 at 12:30
1  
Does the size of the argument make any difference? – Thorbjørn Ravn Andersen May 15 '12 at 13:09
1  
it saves one more array instnatiation – Bozho May 15 '12 at 13:28
List <String> list = ...
String[] array = new String[list.size()];
int i=0;
for(String s: list){
  array[i++] = s;
}
share|improve this answer
This works, but isn't super efficient, and duplicates functionality in the accepted answer with extra code. – Alan Delimon Feb 8 at 15:26

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.