在编写Java程序中,经常要用的一个转换就是数组和List对象之间的互转
[代码]数组->List
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);
}
}
}
2. [代码]比较傻的做法
String[] words = { ... };
List<String> list = new ArrayList<String>(words.length);
for (String s : words) {
list.add(s);
}
3. [代码]这方法也还可以
import java.util.Collections;
List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};
Collections.addAll(myList, myStringArray);
|
|