Java 集合框架源码解读之Vector

  • 数组中实际元素数量

*/

protected int elementCount;

/**

  • The amount by which the capacity of the vector is automatically

  • incremented when its size becomes greater than its capacity. If

  • the capacity increment is less than or equal to zero, the capacity

  • of the vector is doubled each time it needs to grow.

  • @serial

  • 每次扩容的大小,如果为 0,每次扩容为原来的两倍

*/

protected int capacityIncrement;

/**

  • Constructs an empty vector with the specified initial capacity and

  • capacity increment.

  • @param initialCapacity the initial capacity of the vector

  • @param capacityIncrement the amount by which the capacity is

  •                          increased when the vector overflows
    
  • @throws IllegalArgumentException if the specified initial capacity

  •     is negative
    
  • 初始化容器,并指定初始容量和扩容大小

*/

public Vector(int initialCapacity, int capacityIncrement) {

super();

if (initialCapacity < 0)

throw new IllegalArgumentException("Illegal Capacity: "+

initialCapacity);

this.elementData = new Object[initialCapacity];

this.capacityIncrement = capacityIncrement;

}

/**

  • Constructs an empty vector with the specified initial capacity and

  • with its capacity increment equal to zero.

  • @param initialCapacity the initial capacity of the vector

  • @throws IllegalArgumentException if the specified initial capacity

  •     is negative
    
  • 初始化容器并指定初始容量,扩容大小默认为0(即每次扩容为原来的两倍)

*/

public Vector(int initialCapacity) {

this(initialCapacity, 0);

}

/**

  • Constructs an empty vector so that its internal data array

  • has size {@code 10} and its standard capacity increment is

  • zero.

  • 初始化容器并指定初始容量10,扩容大小默认为0(即每次扩容为原来的两倍)

*/

public Vector() {

this(10);

}

/**

  • Constructs a vector containing the elements of the specified

  • collection, in the order they are returned by the collection’s

  • iterator.

  • @param c the collection whose elements are to be placed into this

  •   vector
    
  • @throws NullPointerException if the specified collection is null

  • @since 1.2

  • 以一个集合为默认元素,初始化容器

*/

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);

}

3:增删查改

增加和删除操作才算作结构的改变,才会记录修改次数。修改时不记录修改次数。

3.1:增加元素

/**

  • Appends the specified element to the end of this Vector.

  • @param e element to be appended to this Vector

  • @return {@code true} (as specified by {@link Collection#add})

  • @since 1.2

  • 插入元素到数组的末尾

*/

public synchronized boolean add(E e) {

modCount++;

ensureCapacityHelper(elementCount + 1);

elementData[elementCount++] = e;

return true;

}

/**

  • Adds the specified component to the end of this vector,

  • increasing its size by one. The capacity of this vector is

  • increased if its size becomes greater than its capacity.

  • This method is identical in functionality to the

  • {@link #add(Object) add(E)}

  • method (which is part of the {@link List} interface).

  • @param obj the component to be added

  • 与add(E e)方法相同,但是没有返回值

*/

public synchronized void addElement(E obj) {

modCount++;

ensureCapacityHelper(elementCount + 1);

elementData[elementCount++] = obj;

}

/**

  • This implements the unsynchronized semantics of ensureCapacity.

  • Synchronized methods in this class can internally call this

  • method for ensuring capacity without incurring the cost of an

  • extra synchronization.

  • @see #ensureCapacity(int)

  • 超出容量即进行扩增

*/

private void ensureCapacityHelper(int minCapacity) {

// overflow-conscious code

if (minCapacity - elementData.length > 0)

grow(minCapacity);

}

/**

  • The maximum size of array to allocate.

  • Some VMs reserve some header words in an array.

  • Attempts to allocate larger arrays may result in

  • OutOfMemoryError: Requested array size exceeds VM limit

  • 允许数组最大容量值

*/

最后

本人也收藏了一份Java面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们

目录:

全靠这套面试题,才让我有惊无险美团二面拿offer  (面经解析)

Java面试核心知识点

一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!

全靠这套面试题,才让我有惊无险美团二面拿offer  (面经解析)

Java面试核心知识点

已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了

全靠这套面试题,才让我有惊无险美团二面拿offer  (面经解析)

面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们

目录:

[外链图片转存中…(img-ZOxZbB42-1714319185850)]

Java面试核心知识点

一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!

[外链图片转存中…(img-VSan2zUN-1714319185851)]

Java面试核心知识点

已经有读者朋友靠着这一份Java面试知识点指导拿到不错的offer了

[外链图片转存中…(img-UW1oIA5H-1714319185851)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值