-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>
0

4 Answers 4

2

Convert String Array to ArrayList as

String[] results = new String[] {"Java", "Android", "Hello"};
ArrayList<String> strlist = 
     new ArrayList<String>(Arrays.asList(results));
Sign up to request clarification or add additional context in comments.

1 Comment

The method putStringArrayListExtra(String, ArrayList<String>) in the type Intent is not applicable for the arguments (String, List<String>)intent.putStringArrayListExtra("stock_list", strings);
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.

1 Comment

Note that the resulting List of Arrays.asList() will have a fixed size, adding elements will result in an UnsupportedOperationException.
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!

Comments

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.