-1

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>
Community
  • 1
  • 1
user1007666
  • 19
  • 1
  • 5

4 Answers4

2

Convert String Array to ArrayList as

String[] results = new String[] {"Java", "Android", "Hello"};
ArrayList<String> strlist = 
     new ArrayList<String>(Arrays.asList(results));
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • The method putStringArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, List)intent.putStringArrayListExtra("stock_list", strings); – user1007666 Dec 08 '12 at 14:19
1

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.

micha
  • 47,774
  • 16
  • 73
  • 80
  • Note that the resulting List of `Arrays.asList()` will have a fixed size, adding elements will result in an `UnsupportedOperationException`. – nkr Dec 08 '12 at 16:54
0

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!

Xander
  • 5,487
  • 14
  • 49
  • 77
0

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))
Christoffer Hammarström
  • 27,242
  • 4
  • 49
  • 58