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

2022-08-31 07:55:42

可能的重复:
在 Java 中将数组分配给 ArrayList

我需要将 a 转换为 a,但我不知道该怎么做String[]ArrayList<String>

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

基本上,我想变成一个.filesOrigArrayList


答案 1

您可以执行以下操作:

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

答案 2

喜欢这个:

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