197

我想将String数组转换为ArrayList. 例如字符串数组是这样的:

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

如何将此字符串数组转换为 ArrayList?

4

5 回答 5

325

为此使用此代码,

import java.util.Arrays;  
import java.util.List;  
import java.util.ArrayList;  

public class StringArrayTest {

   public static void main(String[] args) {  
      String[] words = {"ace", "boom", "crew", "dog", "eon"};  

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

      for (String e : wordList) {  
         System.out.println(e);  
      }  
   }  
}
于 2012-05-10T08:41:48.537 回答
180
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) );
于 2012-05-10T08:43:15.787 回答
53

使用集合#addAll()

String[] words = {"ace","boom","crew","dog","eon"};
List<String> arrayList = new ArrayList<>(); 
Collections.addAll(arrayList, words); 
于 2012-05-10T11:25:59.223 回答
27
String[] words= new String[]{"ace","boom","crew","dog","eon"};
List<String> wordList = Arrays.asList(words);
于 2012-05-10T08:43:13.947 回答
12

在大多数情况下List<String>应该足够了。无需创建ArrayList

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

...

String[] words={"ace","boom","crew","dog","eon"};
List<String> l = Arrays.<String>asList(words);

// if List<String> isnt specific enough:
ArrayList<String> al = new ArrayList<String>(l);
于 2012-05-10T08:50:03.460 回答