Java基础:数组Array转成List的几种方法
代码] 数组->List (StringArrayTest.java)
view sourceprint?01 import java.util.Arrays;
02 import java.util.List;
03 import java.util.ArrayList;
05 public class StringArrayTest
06 {
07 public static void main(String[] args)
08 {
09 String[] words = {"ace", "boom", "crew", "dog", "eon"};
11 List<String> wordList = Arrays.asList(words);
13 for (String e : wordList)
14 {
15 System.out.println(e);
16 }
17 }
18 }
[代码] 比较傻的做法
view sourceprint?
1 String[] words = { ... };
2 List<String> list = new ArrayList<String>(words.length);
3 for (String s : words) {
4 list.add(s);
5 }
[代码] 这方法也还可以
view sourceprint?
1 import java.util.Collections;
3 List myList = new ArrayList();
4 String[] myStringArray = new String[] {"Java", "is", "Cool"};
6 Collections.addAll(myList, myStringArray);