Java自定义集合类的实现与比较
1. 扩展预定义集合类(MyIntCollection)
为了说明扩展预定义集合类的方法,我们将扩展 ArrayList 类来创建一个名为 MyIntCollection 的集合类。由于继承的特性, MyIntCollection 类型的对象至少能够响应 ArrayList 可以响应的所有服务请求。不过,我们希望 MyIntCollection 类能做一些额外的工作,即跟踪给定 MyIntCollection 实例中存储的最小和最大整数值。
以下是 MyIntCollection 类的完整代码:
import java.util.ArrayList;
public class MyIntCollection extends ArrayList<Integer> {
private int smallestInt;
private int largestInt;
private int total;
public MyIntCollection() {
super();
total = 0;
}
public boolean add(int i) {
if (this.isEmpty()) {
smallestInt = i;
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



