Core Java (二十五) List与Array的相互转化,Set与Array的相互转换

释放双眼,带上耳机,听听看~!

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(&quot;Jim&quot;);
10      Employee bob = new Employee(&quot;Bob&quot;);
11      Employee gin = new Employee(&quot;Gin&quot;);
12     
13     
14      /************List to Array********************************/
15      List&lt;Employee&gt; staff = new ArrayList&lt;Employee&gt;();
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&lt;Employee&gt; 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 &quot;Name:&quot; + name;
50  }
51 
52  private String name = &quot;&quot;;//实例域初始化
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(&quot;Jim&quot;);
11      Employee bob = new Employee(&quot;Bob&quot;);
12      Employee gin = new Employee(&quot;Gin&quot;);
13     
14     
15      /************Set to Array********************************/
16      Set&lt;Employee&gt; staff = new HashSet&lt;Employee&gt;();
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&lt;Employee&gt; 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 &quot;Name:&quot; + name;
51  }
52 
53  private String name = &quot;&quot;;//实例域初始化
54}
55

给TA打赏
共{{data.count}}人
人已打赏
安全技术

详解Node.js API系列 Crypto加密模块(1)

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索