18

How do I convert String Array to Array List:

String[] to ArrayList<String>

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38

5 Answers5

5

Try this:

String [] strings = new String [] {"stack", "overflow" };
List<String> stringList = new ArrayList<String>(Arrays.asList(strings)); 
Joel Fernandes
  • 4,776
  • 2
  • 26
  • 48
3

Try this one

private ArrayList<String> list = new ArrayList<String>();
list.clear();

for(int i=0;i<StringArray.length;i++)
{
    list.add(StringArray[i]);
}
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
ritesh4326
  • 687
  • 7
  • 9
0

Try this..

String[] arr = { "40", "50", "60", "70", "80", "90", "100", };

ArrayList<String> arr_list = new ArrayList<String>();

for (int i = 0; i < arr.length; i++)
    arr_list.add(arr[i]);

or

ArrayList<String> arr_list = new ArrayList<String>(Arrays.asList(arr)); 
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

Try this:

String[] words = {"ace", "boom", "crew", "dog", "eon"};  

List<String> wordList = Arrays.asList(words);  

for (String e : wordList)  
{  
    System.out.println(e);  
}  
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
Avijit
  • 3,834
  • 4
  • 33
  • 45
0

You can do

  • Use the Arrays.asList() method

    List<String> list = Arrays.asList(strings);
    
  • Create a new ArrayList and copy the elements of the array (not recommended)

    List<String> list = new ArrayList<String>();
    
    for (String str : strings)
    {
        list.add(str);
    }
    

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91