关于java:将String数组转换为ArrayList

Convert String array to ArrayList

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Converting array to list in Java

我想把String数组转换成ArrayList。例如,字符串数组如下:

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

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


使用此代码,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);  
      }  
   }  
}


1
new ArrayList( Arrays.asList( new String[]{"abc","def"} ) );


使用集合addall()

1
2
3
String[] words = {"ace","boom","crew","dog","eon"};
List<String> arrayList = new ArrayList<>();
Collections.addAll(arrayList, words);


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


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

1
2
3
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

1
2
3
4
5
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);