Java容器类源码-Vector的最全的源码分析(二)

Java容器类源码-Vector的最全的源码分析(二)

三、源码解读

1. 继承、实现

extends:AbstractList<E>
implements:List<E>, RandomAccess, Cloneable, java.io.Serializable

2. 全局变量

(1) 存放数据的数组

protected Object[] elementData;

(2) 存放数量

protected int elementCount;

(3) 容量增量

protected int capacityIncrement;

3. 构造方法

(1) 不带参数的构造方法

public Vector() {
 this(10);
}

(2) 带初始化容量大小的构造方法

/**
 * @param initialCapacity 初始化容量大小
 */
public Vector(int initialCapacity) {
 this(initialCapacity, 0);
}

(3) 带初始化容量和容量增量的构造方法

/**
 * @param initialCapacity	初始化容量大小
 * @param capacityIncrement 容量增量
 */
public Vector(int initialCapacity, int capacityIncrement) {
 super();
 if (initialCapacity < 0)
 throw new IllegalArgumentException("Illegal Capacity: "+
 initialCapacity);
 this.elementData = new Object[initialCapacity];
 this.capacityIncrement = capacityIncrement;
}
(4) 带Colleciton参数的构造方法
/**
 * @param c 将c转为Vector
 */
public Vector(Collection<? extends E> c) {
 elementData = c.toArray();
 elementCount = elementData.length;
 // c.toArray might (incorrectly) not return Object[] (see 6260652)
 if (elementData.getClass() != Object[].class)
 elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}

4. 方法

(1) public synchronized void copyInto(Object[] anArray)

源码解释:

通过JNI调用c++库的arraycopy方法,实现将anArray数组复制到当前的Vector。

 public synchronized void copyInto(Object[] anArray) {
 System.arraycopy(elementData, 0, anArray, 0, elementCount);
 }

(2) public synchronized void trimToSize()

源码解释:

用以优化Vector的内存,我们都知道,Vector的每次容量增量是当前大小的2倍,但是当我们没法用完申请的这么多内存时,我们可以通过调用这个方法用以将不需要的内存释放掉。

 public synchronized void trimToSize() {
 modCount++;		// 修改次数增加
 int oldCapacity = elementData.length;	// 获取到当前的申请的内存大小
 if (elementCount < oldCapacity) {		// 如果当前存放的数量比申请的内存要少
 elementData = Arrays.copyOf(elementData, elementCount);	// 重新为这个数组申请elementCount大小内存,并存放这些数据
 }
 }

(3) public synchronized void ensureCapacity(int minCapacity)

源码解释:

当要扩容时,会调用此方法,保证当前容量能存放得下所需要存放的元素数量。如果不够时,会调用grow()方法进行扩容。

 public synchronized void ensureCapacity(int minCapacity) {
 if (minCapacity > 0) {
 modCount++;
 ensureCapacityHelper(minCapacity);
 }
 }
 private void ensureCapacityHelper(int minCapacity) {
 // overflow-conscious code
 if (minCapacity - elementData.length > 0)
 grow(minCapacity);	// 如果当前容量比数组的长度要小时,调用grow扩容
 }
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
	/**
	 * Vector的扩容方法, 每次扩容至2倍
	 * @param minCapacity
	 */
	private void grow(int minCapacity) {
 // overflow-conscious code
 int oldCapacity = elementData.length;	// 获取到数组的长度
 int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); //如果没有初始化capacityIncrement,则增加至两倍
 if (newCapacity - minCapacity < 0)	
 newCapacity = minCapacity;
 if (newCapacity - MAX_ARRAY_SIZE > 0)	// 如果扩容后的容量超过了最大容量
 newCapacity = hugeCapacity(minCapacity);	// 调用hugeCapacity方法
 elementData = Arrays.copyOf(elementData, newCapacity);	// 将数组复制到扩容后的新内存中去
 }
 private static int hugeCapacity(int minCapacity) {	
 if (minCapacity < 0) // overflow
 throw new OutOfMemoryError();
 return (minCapacity > MAX_ARRAY_SIZE) ?
 Integer.MAX_VALUE :
 MAX_ARRAY_SIZE;
 }

(4) public synchronized void setSize(int newSize)

源码解释:

修改容量,当newSize比数组的长度要大时,将其复制到新的内存区域,如果要小的话,则从newSize位置到数组的最后一个位置的所有元素置为空。

 public synchronized void setSize(int newSize) {
 modCount++;
 if (newSize > elementCount) {
 ensureCapacityHelper(newSize);
 } else {
 for (int i = newSize ; i < elementCount ; i++) {
 elementData[i] = null;
 }
 }
 elementCount = newSize;
 }

(5) public synchronized int capacity()

源码解释:

返回数组长度,即申请内存的长度。

 public synchronized int capacity() {
 return elementData.length;
 }

(6) public synchronized int size()

源码解释:

返回数组已经使用的长度,即存放的数据个数。

 public synchronized int size() {
 return elementCount;
 }

(7) public synchronized boolean isEmpty()

源码解释:

判空,如果数量为0,即为empty。

 public synchronized boolean isEmpty() {
 return elementCount == 0;
 }

(8) public Enumeration<E> elements()

源码解释:

返回一个Enumeration对象的序列。Enumeration只有两个方法,hasMoreElements()和nextElement(),它只能从首个元素遍历到最后一个元素,并不能根据位置拿到具体的元素。

 public Enumeration<E> elements() {
 return new Enumeration<E>() {
 int count = 0;
 
 public boolean hasMoreElements() {
 return count < elementCount;
 }
 
 public E nextElement() {
 synchronized (Vector.this) {
 if (count < elementCount) {
 return elementData(count++);
 }
 }
 throw new NoSuchElementException("Vector Enumeration");
 }
 };
 }

(9) public boolean contains(Object o)

源码解释:

是否包含对象o。调用indexOf判断是否存在。

 public boolean contains(Object o) {
 return indexOf(o, 0) >= 0;
 }
 public int indexOf(Object o) {
 return indexOf(o, 0);
 }

(10) public synchronized int indexOf(Object o, int index)

源码解释:

判断o是否为空,如果为空,则遍历是否存在值为空的元素;不为空,判断是否存在和o相等的元素。

 public synchronized int indexOf(Object o, int index) {
 if (o == null) {
 for (int i = index ; i < elementCount ; i++)
 if (elementData[i]==null)
 return i;
 } else {
 for (int i = index ; i < elementCount ; i++)
 if (o.equals(elementData[i]))
 return i;
 }
 return -1;
 }

(11) public synchronized int lastIndexOf

源码解释:

获取该元素所处的最后一个位置,不存在则返回-1

 public synchronized int lastIndexOf(Object o) {
 return lastIndexOf(o, elementCount-1);
 }
 public synchronized int lastIndexOf(Object o, int index) {
 if (index >= elementCount)
 throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
 
 if (o == null) {
 for (int i = index; i >= 0; i--)
 if (elementData[i]==null)
 return i;
 } else {
 for (int i = index; i >= 0; i--)
 if (o.equals(elementData[i]))
 return i;
 }
 return -1;
 }

(12) public synchronized E elementAt(int index)

源码解释:

返回这个位置的元素。其实就是判断数组的index位置是否有元素

 public synchronized E elementAt(int index) {
 if (index >= elementCount) {
 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
 }
 
 return elementData(index);
 }
 E elementData(int index) {
 return (E) elementData[index];
 }

(13) public synchronized E firstElement()

源码解释:

返回数组第0个位置的元素。

 public synchronized E firstElement() {
 if (elementCount == 0) {
 throw new NoSuchElementException();
 }
 return elementData(0);
 }

(14) public synchronized E lastElement()

源码解释:

返回数组最后一个位置的元素。

 public synchronized E lastElement() {
 if (elementCount == 0) {
 throw new NoSuchElementException();
 }
 return elementData(elementCount - 1);
 }

(15) public synchronized void setElementAt(E obj, int index)

源码解释:

修改第index位置的值为obj。其实就是数组赋值。

 public synchronized void setElementAt(E obj, int index) {
 if (index >= elementCount) {
 throw new ArrayIndexOutOfBoundsException(index + " >= " +
 elementCount);
 }
 elementData[index] = obj;
 }

每天都在分享文章,也每天都有人想要我出来给大家分享下怎么去学习Java。大家都知道,我们是学Java全栈的,大家就肯定以为我有全套的Java系统教程。没错,我是有Java全套系统教程,进扣裙【47】974【9726】所示,进群的时候记得表明自己想要学习什么,不要用小号,这样小编才好给你们发定向资源,今天小编就免费送!~

Java容器类源码-Vector的最全的源码分析(二)

“我们相信人人都可以成为一个程序员,现在开始,找个师兄,带你入门,学习的路上不再迷茫。这里是ja+va修真院,初学者转行到互联网行业的聚集地。"

猜你喜欢

转载自blog.csdn.net/qq_41552245/article/details/86231211
今日推荐