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 }
2 泛型
3 异常