菜鸟的Java源代码分析--Vector类

Vector类:


为了更好的从宏观上理解Vector,我们可以首先看一下Vector的父类以及实现的一些Interface:



 

Vector作为一种容器,实现了Collection这个接口,并继承AbstractList这个“直接父类”。同时,我们还能看到,Vector和他的父类AbstractList都实现了List这个接口,表明Vector是一种List类型的容器(其他的有比如Map型)。
综上,用集合的思想来看Vector的逻辑地位:Collection > List > Vector ,Vector是所有容器的一条分支。

 

由UML图,可以看到Vector的这个类在设计时的逻辑构架,作为一个菜鸟,除了五花八门的算法,这种高级的面向对象的设计思想,是初学者更应该去把握的。 

下面来Trace Code:Interface Collection:
Collection是所有容器的根,定义了了Collection应该去实现的方法Method,见下图:


 

public abstract class AbstractCollection implements Collection {

    protected AbstractCollection() {
        super();
    }
    public boolean add(E object) {
        throw new UnsupportedOperationException();
    }

    public boolean addAll(Collection extends E> collection) {
        boolean result = false;
        Iterator extends E> it = collection.iterator();
        while (it.hasNext()) {
            if (add(it.next())) {
                result = true;
            }
        }
        return result;
    }

    public void clear() {
        Iterator it = iterator();
        while (it.hasNext()) {
            it.next();
            it.remove();
        }
    }

    public boolean contains(Object object) {
        Iterator it = iterator();
        if (object != null) {
            while (it.hasNext()) {
                if (object.equals(it.next())) {
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (it.next() == null) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean containsAll(Collection> collection) {
        Iterator> it = collection.iterator();
        while (it.hasNext()) {
            if (!contains(it.next())) {
                return false;
            }
        }
        return true;
    }

    public boolean isEmpty() {
        return size() == 0;
    }
    public abstract Iterator iterator();

    public boolean remove(Object object) {
        Iterator> it = iterator();
        if (object != null) {
            while (it.hasNext()) {
                if (object.equals(it.next())) {
                    it.remove();
                    return true;
                }
            }
        } else {
            while (it.hasNext()) {
                if (it.next() == null) {
                    it.remove();
                    return true;
                }
            }
        }
        return false;
    }

    public boolean removeAll(Collection> collection) {
        boolean result = false;
        Iterator> it = iterator();
        while (it.hasNext()) {
            if (collection.contains(it.next())) {
                it.remove();
                result = true;
            }
        }
        return result;
    }

    public boolean retainAll(Collection> collection) {
        boolean result = false;
        Iterator> it = iterator();
        while (it.hasNext()) {
            if (!collection.contains(it.next())) {
                it.remove();
                result = true;
            }
        }
        return result;
    }
    public abstract int size();
    public Object[] toArray() {
        int size = size(), index = 0;
        Iterator> it = iterator();
        Object[] array = new Object[size];
        while (index  T[] toArray(T[] contents) {
        int size = size(), index = 0;
        if (size > contents.length) {
            Class> ct = contents.getClass().getComponentType();
            contents = (T[]) Array.newInstance(ct, size);
        }
        for (E entry : this) {
            contents[index++] = (T) entry;
        }
        if (index  it = iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if (next != this) {
                buffer.append(next);
            } else {
                buffer.append("(this Collection)"); //$NON-NLS-1$
            }
            if (it.hasNext()) {
                buffer.append(", "); //$NON-NLS-1$
            }
        }
        buffer.append(']');
        return buffer.toString();
    }
}
 

 

//AbstractCollection 抽象的实现了Collection这个接口的所有方法,没有存在任何具体的Method,因为所有遍历过程都依赖于自身的迭代器。

举个例子:

    public void clear() {
        Iterator it = iterator();
        while (it.hasNext()) {
            it.next();
            it.remove();
        }
    }

照着设计者的意思,加黑这句代码iterator()指望着子类去实现了,所有的框架都给你搭好了,子类或者任何具体的容器要去实现这个迭代器,更进一步说,具体容器的数据结构与迭代器紧密相关,文邹邹说起来这叫耦合度高。在这里,一切都是个模子。小总结:

1、方法却是一个抽象的方法,显然设计者的目的是让所有的ConcreteCollection(具体Collection)去继承抽象的Collection。

2、容器的结构与迭代方式都因自身的种类而不同。

 

困死了,待续。。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值