0

I have a resource string array that I want to put into an ArrayList, however the datatype that comes back is a String array. The string[] cannot be directly cast to ArrayList w/o receiving the following error:

Cannot cast from String[] to ArrayList

How to I convert the String[] datatype to ArrayList?

Edit

I have an adapter whose constructor takes an ArrayList and a resource string-array that I want to populate it.

3 Answers 3

9

If you can make do with a List<String> (and not specifically an ArrayList<String>), you can use:

List<String> list = Arrays.asList(stringArray);

Otherwise, you can do this:

ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));

However, the latter is less efficient (in both time and object creation count) than the other suggested solutions. Its only benefit is that it keeps the code down to one line and is easy to comprehend at a glance.

Sign up to request clarification or add additional context in comments.

4 Comments

List and ArrayList are not the same.
@AndroidAddict - I realize that. That's why I qualified the first suggestion. However, ArrayList is a subtype of List, so it's quite possible that this will work.
@AndroidAddict what exactly is your ideal solution? You will not be able to do any cast directly from an array to an array list. There are plenty of sufficiently efficient answers here.
You just gave it too me. I have an adapter whose constructor takes an ArrayList<String> and a resource string-array that I want to pass in. So this works: MyListAdapter(this, resourceId, new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.string_array_list))));
2
public ArrayList<String> arrayToArrayList(String[] array){
    ArrayList<String> arrayList = new ArrayList<String>(array.length);
    for(String string : array){
        arrayList.add(string);
    }

    return arrayList;
}

3 Comments

I'm sure this works, but the other answer is more efficient. Thank you though!
Efficiency would be improved if you constructed arrayList with initial capacity set to array.length. @AndroidAddict - Why do you think this is not efficient?
Efficient from amount of coding necessary. This project is extremely large and anywhere we can economize on coding with a reasonable performance trade-off we will. That is the only perspective I was referring to, not that it won't execute efficiently. Just not as terse.
2

how about this

import java.util.Collections; 

String[] myStringArray = new String[] {"foo", "bar", "baz"}; 
List myList = new ArrayList(myStringArray.length);
Collections.addAll(myList, myStringArray); 

4 Comments

Okay this works, but is there a more efficient (terse) way? If so, please edit your answer to reflect such. I would ideally like to perform the conversion in one line w/o creating another variable.
@AndroidAddict none that I know of, you can keep the question open and see if anybody comes up with anything better, but if you are looking for terse, java is probably not the language for you =).
There really isn't another way other than, somewhere along the way, creating a new ArrayList. There's no special way to cast an array to a list.
Efficiency would be improved if you constructed myList with initial capacity set to myStringArray.length.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.