1. [代码]数组->List (StringArrayTest.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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. [代码]比较傻的做法
1
2
3
4
5
|
String[] words = { ... };
List<String> list =
new
ArrayList<String>(words.length);
for
(String s : words) {
list.add(s);
}
|
3. [代码]这方法也还可以
1
2
3
4
5
6
|
import
java.util.Collections;
List myList =
new
ArrayList();
String[] myStringArray =
new
String[] {
"Java"
,
"is"
,
"Cool"
};
Collections.addAll(myList, myStringArray);
|