如题所述
代码片段(3)[全屏查看所有代码]1. [代码]数组->List (StringArrayTest.java) 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);
温馨提示:答案为网友推荐,仅供参考