How do i convert String array to java.util.List?

share|improve this question
4  
What have you tried? What didn't work? – Oded May 17 '11 at 6:01
2  
common question, you can get it by googling also – developer May 17 '11 at 6:03
4  
now google brings you here for this question :) – Sikorski Nov 27 '12 at 7:46
Google brought me here too. – Susie Apr 4 at 18:35

3 Answers

List<String> strings = Arrays.asList(new String[]{"one", "two", "three"});

This is a list view of the array, the list is partly unmodifiable, you can't add or delete elements. But the time complexity is O(1).

If you want a modifiable a List:

List<String> strings = 
     new ArrayList<String>(Arrays.asList(new String[]{"one", "two", "three"}));

This will copy all elements from the source array into a new list (complexity: O(n))

share|improve this answer
Thanks for the complexity info! :) – damned May 23 '12 at 2:18
import java.util.Collections;

List myList = new ArrayList();
String[] myArray = new String[] {"Java", "Util", "List"};

Collections.addAll(myList, myArray);
share|improve this answer

Use the static List list = Arrays.asList(stringArray) or you could just iterate over the array and add the strings to the list.

share|improve this answer

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.