List与Array的相互转化
List转化成Array
调用了List的toArray方法,有两个同名方法,其中Object[] toArray()返回一个Object类型的数组,但使用起来很不方便。另外一个是
public <T> T[] toArray(T[] a),返回一个泛型数组T[].
Array转化成List
调用了Arrays类的静态方法asList,返回一个包装了普通java数组的List包装器。返回的对象不是ArrayList,而是一个视图对象,实现了Serializable接口即可序列化,并且实现了RandomAccess接口。
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 1package com.xujin;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.List;
6
7public class LinkedListTest{
8 public static void main(String...arg){
9 Employee jim = new Employee("Jim");
10 Employee bob = new Employee("Bob");
11 Employee gin = new Employee("Gin");
12
13
14 /************List to Array********************************/
15 List<Employee> staff = new ArrayList<Employee>();
16 staff.add(jim);
17 staff.add(bob);
18 staff.add(gin);
19 System.out.println(staff);//[Name:Jim, Name:Bob, Name:Gin]
20
21 Employee[] e = new Employee[staff.size()];
22 staff.toArray(e);
23 for(Employee em : e)
24 System.out.println(em);
25 /*
26 * Name:Jim
27 * Name:Bob
28 * Name:Gin
29 * */
30
31
32 /************Array to List********************************/
33 List<Employee> arrayToList = Arrays.asList(e);
34 System.out.println(arrayToList.get(2));//Name:Gin
35
36 }
37}
38
39class Employee{
40 public Employee(String name){
41 this.name = name;
42 }
43
44 public String getName(){
45 return this.name;
46 }
47
48 public String toString(){
49 return "Name:" + name;
50 }
51
52 private String name = "";//实例域初始化
53}
54
Set与Array的相互转换
与上面的相仿,Set转化成Array是调用的toArray的泛型方法,而Array转换成Set是调用的Array的asList方法。
例子:
此例子与上面的几乎一样,唯一的不同就是
/************Set to Array********************************/
Set<Employee> staff = new HashSet<Employee>();
结果也都一样。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 1package com.xujin;
2
3import java.util.Arrays;
4import java.util.HashSet;
5import java.util.List;
6import java.util.Set;
7
8public class LinkedListTest{
9 public static void main(String...arg){
10 Employee jim = new Employee("Jim");
11 Employee bob = new Employee("Bob");
12 Employee gin = new Employee("Gin");
13
14
15 /************Set to Array********************************/
16 Set<Employee> staff = new HashSet<Employee>();
17 staff.add(jim);
18 staff.add(bob);
19 staff.add(gin);
20 System.out.println(staff);//[Name:Jim, Name:Bob, Name:Gin]
21
22 Employee[] e = new Employee[staff.size()];
23 staff.toArray(e);
24 for(Employee em : e)
25 System.out.println(em);
26 /*
27 * Name:Jim
28 * Name:Bob
29 * Name:Gin
30 * */
31
32
33 /************Array to Set********************************/
34 List<Employee> arrayToList = Arrays.asList(e);
35 System.out.println(arrayToList.get(2));//Name:Gin
36
37 }
38}
39
40class Employee{
41 public Employee(String name){
42 this.name = name;
43 }
44
45 public String getName(){
46 return this.name;
47 }
48
49 public String toString(){
50 return "Name:" + name;
51 }
52
53 private String name = "";//实例域初始化
54}
55