如何将String []转换为ArrayList <string> </string>

时间:2012-04-19 15:00:39

标签: java android string arraylist

  

可能重复:
  Assigning an array to an ArrayList in Java

我需要将String[]转换为ArrayList<String>,我不知道如何

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

基本上我想将filesOrig转换为ArrayList

6 个答案:

答案 0 :(得分:318)

您可以执行以下操作:

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

答案 1 :(得分:29)

像这样:

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

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

答案 2 :(得分:14)

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

返回的列表将由数组支持,它就像一个桥,因此它将是固定大小。

答案 3 :(得分:6)

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

答案 4 :(得分:4)

您可以循环所有数组并添加到ArrayList:

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

或者使用Arrays.asList(T... a)作为评论发布。

答案 5 :(得分:2)

您可以执行类似

的操作
  

MyClass [] arr = myList.toArray(new MyClass [myList.size()]);