有三种方法可以实现
String[] words = {"ace", "boom", "crew", "dog", "eon"};
List wordList = Arrays.asList(words);
for循环
List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};
Collections.addAll(myList, myStringArray);
转载自:http://www.oschina.net/code/snippet_8676_2652
在编写Java程序中,经常要用的一个转换就是数组和List对象之间的互转
[代码] 数组->List (StringArrayTest.java)
01 |
import java.util.Arrays; |
02 |
import java.util.List; |
03 |
import java.util.ArrayList; |
05 |
public class StringArrayTest |
07 |
public static void main(String[] args) |
09 |
String[] words = { "ace" , "boom" , "crew" , "dog" , "eon" }; |
11 |
List<String> wordList = Arrays.asList(words); |
14 |
for (String e : wordList) |
16 |
System.out.println(e); |
[代码] 比较傻的做法
String[] words = { ... };
List<String> list = new ArrayList<String>(words.length);
for (String s : words) {
list.add(s);
}
[代码] 这方法也还可以
import java.util.Collections;
List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};
Collections.addAll(myList, myStringArray);