集合Collection
泛型Generics
案例
问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class IceCreamBox { private int id; private IceCream iceCream; private double weight; public void put(IceCream iceCream) { this.iceCream = iceCream; } public boolean isEmpty() { return iceCream == null; } public IceCream takeOut() { IceCream iceCream = this.iceCream; this.iceCream = null; return iceCream; } // getters and setters}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class MilkTeaBox { private int id; private MilkTea milkTea; private double weight; public void put(MilkTea milkTea) { this.milkTea = milkTea; } public boolean isEmpty() { return milkTea == null; } public MilkTea takeOut() { MilkTea milkTea = this.milkTea; this.milkTea = null; return milkTea; } // getters and setters}
代码大面积重叠
解决方案
1*2 3*4 5 6*7*8 9 10 11*12 13 14*15*16*17*18 19 20 21 public class Box <T> { private int id; private T content; private double weight; public void put(T content) { this.content = content; } public boolean isEmpty() { return content == null; } public T takeOut() { T content = this.content; this.content = null; return content; } // getters and setters}
使用
1 2 3 4 5 6 7 public class Driver { public static void main(String[] args) { Box<MilkTea> milkTeaBox = new Box<>(); MilkTea milkTea = new MilkTea(); milkTeaBox.put(milkTea); }}
什么是
将类型中的类型 作为变量
Type Arguments
机制
new 时,传递泛型值,从而确定这个类到底是什么样
再使用,之后 会有方法的对应的类型提示
语法
声明
1 public class Node <T>
赋值
1 new Node<Double>();
使用
1 private T value;
命名规则
一般用一个大写字母, 也可以用单词, 大写开头
赋值规则
不可以赋值基础类型
包装类Wrapper Classes
背景
有些情况下, 一些语法只能接收对象类型
1!new Node<int>();
需要一种结构把基础类型 转成 对象类型
样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Int { private int value; public Int(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; }}
1 2 3 4 5 6 7 8 9 10 public class Driver { public static void main(String[] args) { new Driver().run(); } public void run(){ Int anInt = new Int(3); int value = anInt.getValue(); }}
装箱 和 拆箱Boxing and Unboxing
装箱
1 new Int(3);
拆箱
1anInt.getValue();
配套泛型使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Driver { public static void main(String[] args) { new Driver().run(); } public void run(){ Box<Int> intBox = new Box<Int>(); Int intObject = new Int(10); // 装箱 - 把基础类型放到包装对象里 intBox.setValue(intObject); // 把对象放到业务代码里 Int intObjectOut = intBox.getValue(); // 从业务对象里取出对象 int intValue = intObjectOut.getValue(); // 拆箱 - 把包装对象拆出基础类型 System.out.println(intValue); }}
系统包装类
boolean Boolean byte Byte char Character float Float int Integer long Long short Short double Double
自动装箱拆箱
自动装箱
1 2 Integer a = new Integer(3);Integer a = 3;
自动拆箱
1 2 int value = a.intValue();int value = a;
LinkedList
是什么
系统的 LinkedList
LinkedList
Oracle 官方参考文档
样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Link { public static void main(String[] args) { new Link().run(); } public void run(){ LinkedList<Integer> list = new LinkedList<Integer>(); list.add(6); list.add(7); list.add(3); list.add(8); list.add(4); for (int i = 0; i < list.size(); i++) { int value = list.get(i); System.out.println(value); } }}
内部实现
使用双线链表实现
For Each遍历专用 for 语句
案例
使用前
1 2 3 4 for (int i = 0; i < list.size(); i++) { Integer value = list.get(i); System.out.println(value);}
使用后
1-2-3+4 5 for (int i = 0; i < list.size(); i++) { Integer value = list.get(i);for (Integer value : list) { System.out.println(value);}
语法
for (元素类型 元素变量 : 集合变量) { ... }
for 语句对比
一般 for 可以进行高级控制
标准 for 可以获得当前位置
遍历 for 只能遍历所有元素
数组也可以用
1 2 3 4 int[] values = {6, 7, 3, 8, 4};for (int value : values) { System.out.println(value);}