Vector源码理解
Vector类1.系统的变量
//记录元素的数组protected Object[] elementData;//上面数组的空间长度protected int elementCount;//有效元素数protected int capacityIncrement;2.构造函数
???? 构造方法中主要内容
this.elementData = new Object[10];this.capacityIncrement = 0;?3.add方法
public synchronized boolean add(E e) {modCount++; //验证数组空间是否够用 ensureCapacityHelper(elementCount + 1); //添加进去 elementData[elementCount++] = e; return true;}?
?这里和ArrayList相比就是增加了同步的synchronized。然后这个类的大部分都有同步操作,竟然还包括size这样的方法,让我很不理解,这些类似于只读得操作也需要限制吗?
public synchronized int size()4.比ArrayList多的方法。1.copyInto将此向量的组件复制到指定的数组中。
public synchronized void copyInto(Object[] anArray) {System.arraycopy(elementData, 0, anArray, 0, elementCount);}2.
elements()
返回此向量的组件的枚举,枚举类型以后再分析。3.firstElement() 返回第一个元素。用了synchronized。return (E)elementData[0];4.lastElement() 返回最后一个元素。 用了synchronized。
return (E)elementData[elementCount - 1];
?没什么特别的地方,和ArrayList逻辑非常类似。结束
?
Strack是Vector的子类里面没什么内容,只是封装了几个简单方法,例如:
public synchronized int search(Object o) {int i = lastIndexOf(o);if (i >= 0) { return size() - i;}return -1; }
?
public boolean empty() {return size() == 0; }
?完。
?