How can I convert String[] to ArrayList<String>

191,324

Solution 1

You can do the following:

String [] strings = new String [] {"1", "2" };
List<String> stringList = new ArrayList<String>(Arrays.asList(strings)); //new ArrayList is only needed if you absolutely need an ArrayList

Solution 2

Like this :

String[] words = {"000", "aaa", "bbb", "ccc", "ddd"};
List<String> wordList = new ArrayList<String>(Arrays.asList(words));

or

List myList = new ArrayList();
String[] words = {"000", "aaa", "bbb", "ccc", "ddd"};
Collections.addAll(myList, words);

Solution 3

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

The list returned will be backed by the array, it acts like a bridge, so it will be fixed-size.

Solution 4

List myList = new ArrayList();
Collections.addAll(myList, filesOrig); 

Solution 5

You can loop all of the array and add into ArrayList:

ArrayList<String> files = new ArrayList<String>(filesOrig.length);
for(String file: filesOrig) {
    files.add(file);
}

Or use Arrays.asList(T... a) to do as the comment posted.

Share:
191,324
Alexandre Hitchcox
Author by

Alexandre Hitchcox

Updated on November 08, 2020

Comments

  • Alexandre Hitchcox
    Alexandre Hitchcox over 3 years

    Possible Duplicate:
    Assigning an array to an ArrayList in Java

    I need to convert a String[] to an ArrayList<String> and I don't know how

    File dir = new File(Environment.getExternalStorageDirectory() + "/dir/");
    String[] filesOrig = dir.list();
    

    Basically I would like to transform filesOrig into an ArrayList.

  • zapl
    zapl about 12 years
    ...new ArrayList<String>(filesOrig.length); prevents that the backing array has to be grown in size. Potentially faster.
  • Alexandre Hitchcox
    Alexandre Hitchcox about 12 years
    Thanks, this is the one I used
  • MysticMagicϡ
    MysticMagicϡ over 9 years
    Thanks.. I couldn't use Arrays.asList as I had to add element to list later. addAll worked perfectly :)
  • Bob Lissner
    Bob Lissner over 7 years
    I don't believe that you can then make changes to this list. stringList.remove(1) sure isn't working for me.
  • suriv
    suriv over 7 years
    This answer is wrong and won't even compile.
  • Jack
    Jack over 7 years
    @suriv: the only problem was List instead of ArrayList but the answer is not wrong. Creating a copy of an array to an ArrayList is not always necessary, sometimes you just need to view an array as a list. Mind that creating a real copy, as in new ArrayList<>(Arrays.asList(array)) requires a memory for each element of the list. For example a 100000 Object array requires 800kb of RAM just for the pointers to references.
  • suriv
    suriv over 7 years
    For the benefit of future readers, trying to add or remove elements from this List will throw an exception.
  • Jack
    Jack over 7 years
    @suriv: it's clearly stated in the answer and it's clearly stated in the documentation, in any case thanks for the clarification.
  • Someone Somewhere
    Someone Somewhere almost 6 years
    second option works