Core Java(十三) 泛型数组列表,对象包装器与自动打包,枚举类

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

ArrayList

ArrayList是一个采用类型参数的泛型类,例如ArrayList<Employee>,这是一个保存有Employee类型对象的泛型数组列表。

构造器:


1
2
1ArrayList&lt;Employee&gt; staff = new ArrayList&lt;Employee&gt;();
2

下面是一个ArrayList的例子,实现了泛型数组列表的构造器,add方法,remove方法,set方法等等。


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
56
57
1package com.xujin;
2
3import java.util.ArrayList;
4
5public class Main{
6   public static void main(String...args){
7       ArrayList&lt;Employee&gt; staff = new ArrayList&lt;Employee&gt;(10);
8       //add方法添加元素
9       staff.add(new Employee(&quot;Bob&quot;, 4000));
10      staff.add(new Employee(&quot;Jim&quot;, 5000));
11      staff.add(new Employee(&quot;July&quot;, 8000));
12      staff.add(new Employee(&quot;Aliy&quot;, 6000));
13      Employee e = staff.get(0);
14      System.out.println(e.toString());//com.xujin.Employee[name = Bob, salary = 4000.0]
15     
16      //set方法将某个元素设定,该元素必须是已存在的
17      staff.set(0, new Employee(&quot;Lily&quot;, 10000));
18      System.out.println(staff.get(0));//com.xujin.Employee[name = Lily, salary = 10000.0]
19      System.out.println(staff.size());//4
20     
21      //把数组列表削减到当前尺寸
22      staff.trimToSize();
23     
24      //remove函数实现删除一个元素
25      staff.remove(0);
26      System.out.println(staff.get(0));//com.xujin.Employee[name = Jim, salary = 5000.0]
27      System.out.println(staff.size());//3
28     
29      //add方法插入一个元素,index为3,说明3以及3之后的所有元素都后移一位
30      staff.add(3, new Employee(&quot;Ted&quot;, 6000));
31      for(Employee em: staff){
32          System.out.println(em);
33      }
34      /*
35       *  com.xujin.Employee[name = Jim, salary = 5000.0]
36          com.xujin.Employee[name = July, salary = 8000.0]
37          com.xujin.Employee[name = Aliy, salary = 6000.0]
38          com.xujin.Employee[name = Ted, salary = 6000.0]
39      */
40  }
41}
42
43class Employee{
44  public Employee(String name, double salary){
45      this.name = name;      
46      this.salary = salary;      
47  }  
48 
49  public String toString(){
50      return getClass().getName() + &quot;[name = &quot; + name + &quot;, salary = &quot; + salary + &quot;]&quot;;
51  }
52 
53  //定义变量
54  private double salary;
55  private String name;
56}
57

对象包装器与自动打包

每个基本数据类型都有一个与之对应的类,这些类称为对象包装器类。

对象包装器类:
Integer,Double,Float,Long,Short,Byte,Character,Void,Boolean。注:红色的派生于Number类。

自动打包(autoboxing):


1
2
1list.add(3);//自动变成:;list.add(new Integer(3));
2

自动拆包:


1
2
3
1Integer n = 3;
2n++;
3

1
2
1以上两条语句将实现:编译器自动将Integer类型的对象n拆开,然后进行自增运算,最后再将结果打包到对象包内。
2

另外Integer还有几个常用的静态方法,比如下例中的parseInt方法


1
2
3
4
5
6
7
8
9
10
11
12
1package com.xujin;
2
3public class Main{
4   public static void main(String...args){
5       Integer a = 1000000;
6       System.out.println(a.intValue());//1000000
7       int x = Integer.parseInt(&quot;124324&quot;);
8       int y = Integer.parseInt(&quot;2333&quot;, 8);
9       System.out.println(&quot;x:&quot; + x + &quot;\ny:&quot; + y);//x:124324 y:1243
10  }
11}
12

枚举类


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
1package com.xujin;
2
3public class Main{
4   public static void main(String...args){
5       Size s = (Size)Enum.valueOf(Size.class, &quot;SMALL&quot;);
6      
7       //values方法返回该枚举类所有的枚举值
8       Size[] values = Size.values();     
9       for(Size size: values){
10          System.out.println(size.getAbb());
11      }
12     
13      //ordinal()方法返回枚举常量的位置,从0开始
14      System.out.println(Size.LARGE.ordinal());//2
15  }
16}
17
18enum Size{
19  //这个类有五个实例,分别是以下五个
20  SMALL(&quot;S&quot;),MIDIUM(&quot;M&quot;),LARGE(&quot;L&quot;),EXTRA_LARGE(&quot;XL&quot;);
21 
22  private Size(String abbreviation){
23      this.abbreviation = abbreviation;
24  }
25 
26  public String getAbb(){
27      return abbreviation;
28  }
29 
30  private String abbreviation;
31}
32

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

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

2021-12-21 16:36:11

安全技术

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

2022-1-12 12:36:11

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