Possible Duplicate:
How can I convert String[] to ArrayList<String>
hi please can anyone help me I have :
private String results[];
private ArrayList<String> alist;
I want convert
String results[] to ArrayList<String>
Possible Duplicate:
How can I convert String[] to ArrayList<String>
hi please can anyone help me I have :
private String results[];
private ArrayList<String> alist;
I want convert
String results[] to ArrayList<String>
Convert String Array to ArrayList as
String[] results = new String[] {"Java", "Android", "Hello"};
ArrayList<String> strlist =
new ArrayList<String>(Arrays.asList(results));
You can use the Arrays.asList() method to convert an array to a list.
E.g. List<String> alist = Arrays.asList(results);
Please note that Arrays.asList() returns a List instance, not an ArrayList instance. If you really need an ArrayList instance you can use to the ArrayList constuctor an pass the List instance to it.
Try this:
ArrayList<String> aList = new ArrayList<String>();
for(String s : results){
aList.add(s);
}
What this does is, it constructs an ArrayList of Strings called aList: ArrayList<String> aList = new ArrayList<String>();
And then, for every String in results: String s : results
It add's that string: aList.add(s);.
Hope this helps!
You should use
Arrays.asList(results)
by default, unless you absolutely for some reason must have an ArrayList.
For example, if you want to modify the list, in which case you use
new ArrayList(Arrays.asList(results))