Lesson 08:集合框架,泛型,异常

1 集合框架

Q:数组存储元素时,需要预先设定数组大小,需要动态改变大小时怎么办?

A:借助链表或者集合类。

 

Java集合框架的体系结构(红框白底为接口,红底为类)

 

常见的几种集合类——

  • List结构的集合类:ArrayList类,LinkedList类,Vector类,Stack类
  • Map结构的集合类:HashMap类,HashTable类
  • Set结构的集合类:HashSet类,TreeSet类
  • Queue结构的集合类:Queue接口

1.1 ArrayList

 1 package com.set;
 2 import java.util.ArrayList;
 3 
 4 /**
 5  * 集合类,ArrayList实例
 6  * @author Cheng
 7  *
 8  */
 9 public class Drive {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         Stuff[] lists = new Stuff[10];
14         ArrayList stuffSet1= new ArrayList();
15         ArrayList<Stuff> stuffSet2= new ArrayList(); 
16         Stuff s1 = new Stuff(24, "Susu", 3000);
17         Stuff s2 = new Stuff(25,"Xingxing", 2000);
18         
19         stuffSet1.add(s1);
20         stuffSet2.add(s2);
21         
22         Stuff s3 = (Stuff)stuffSet1.get(0);
23         Stuff s4 = stuffSet2.get(0);
24         
25     }
26 
27 }
28 
29 
30 package com.set;
31 /**
32  * Stuff类
33  * @author Cheng
34  *
35  */
36 public class Stuff {
37     private int age;
38     private String name;
39     private double salary;
40     
41     /**
42      * 
43      * @param age
44      * @param name
45      * @param salary
46      */
47     public Stuff(int age, String name, double salary) {
48         super();
49         this.age = age;
50         this.name = name;
51         this.salary = salary;
52     }
53 
54     public int getAge() {
55         return age;
56     }
57 
58     public void setAge(int age) {
59         this.age = age;
60     }
61 
62     public String getName() {
63         return name;
64     }
65 
66     public void setName(String name) {
67         this.name = name;
68     }
69 
70     public double getSalary() {
71         return salary;
72     }
73 
74     public void setSalary(double salary) {
75         this.salary = salary;
76     }
77 }
ArrayList实例

 

 

2 泛型

3 异常

 

转载于:https://www.cnblogs.com/Captainarsenal/p/7757648.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值