java集合类一-ArrayList与Vector的使用及源码解析

目录

 

1 构造方法

2 常用方法

3 ArrayList扩容

4 Vector与ArrayList的区别


1 构造方法

(1)public ArrayList():初始化一个容量为空的容器

public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

(2)ArrayList(int initialCapacity):初始化一个容量为initialCapacity的容器

public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
        }
}

(3)ArrayList(Collection<? extends E> c):初始化一个具有一些元素的集合类。集合类容量为添入元素个数。

 public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

 

2 常用方法

1)add(Object obj):在容器后面添加元素。

public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
	elementData[size++] = e;
	return true;
}

2)add(int index, Object obj):在指定位置添加元素。

首先检查插入下标,然后确保容量。将插入点之后的元素向后移动一位。

public void add(int index, E element) {
	rangeCheckForAdd(index);
	ensureCapacityInternal(size + 1);  // Increments modCount!!
	System.arraycopy(elementData, index, elementData, index + 1,size - index);
	elementData[index] = element;
	size++;
}

3)addAll(Collection<? extends E> c)

在集合类末尾添加集合类。直接将添加的集合类转化为数组,复制到被添加的集合类末尾。

public boolean addAll(Collection<? extends E> c) {
	Object[] a = c.toArray();
	int numNew = a.length;
	ensureCapacityInternal(size + numNew);  // Increments modCount
	System.arraycopy(a, 0, elementData, size, numNew);
	size += numNew;
	return numNew != 0;
}

4)addAll(int index, Collection<? extends E> c)

在某个位置添加集合类。首先将被添加的集合类的在index处分开,空出需要添加的个数,然后将添加的内容复制到空位

public boolean addAll(int index, Collection<? extends E> c) {
		rangeCheckForAdd(index);

		Object[] a = c.toArray();
		int numNew = a.length;
		ensureCapacityInternal(size + numNew);  // Increments modCount

		int numMoved = size - index;
		if (numMoved > 0)
		System.arraycopy(elementData, index, elementData, index + 														numNew,numMoved);

		System.arraycopy(a, 0, elementData, index, numNew);
		size += numNew;
		return numNew != 0;
	}

5)remove(int index)

通过下标移除某个位置的元素。将该下标之后的元素向前移动一位,空出的末尾掷空,原来index处的对象没有指针引用,会被垃圾回收机制回收

public E remove(int index) {
		rangeCheck(index);

		modCount++;
		E oldValue = elementData(index);

		int numMoved = size - index - 1;
		if (numMoved > 0)
		System.arraycopy(elementData, index+1, elementData, index,numMoved);
		elementData[--size] = null; // clear to let GC do its work

		return oldValue;
	}

6)remove(Object o)

通过对象移除某个位置的元素。遍历ArrayList,利用equals方法判断是否相等,如果相等则移除。

public boolean remove(Object o) {
		if (o == null) {
		for (int index = 0; index < size; index++)
		if (elementData[index] == null) {
			fastRemove(index);
			return true;
		}
	} else {
			for (int index = 0; index < size; index++)
			if (o.equals(elementData[index])) {
				fastRemove(index);
				return true;
			}
		}
		return false;
	}

3 ArrayList扩容

ArrayList添加元素的时候,如果元素个数超过内部数组长度,则首先选择扩充到原来的1.5倍,如果还不够则扩充到添加后元素的个数。核心源码如下:

minCapacity为添加元素后集合中元素总数。
	private void grow(int minCapacity) {
		// overflow-conscious code
		int oldCapacity = elementData.length;
		int newCapacity = oldCapacity + (oldCapacity >> 1);
		if (newCapacity - minCapacity < 0)
		newCapacity = minCapacity;
		if (newCapacity - MAX_ARRAY_SIZE > 0)
		newCapacity = hugeCapacity(minCapacity);
		// minCapacity is usually close to size, so this is a win:
		elementData = Arrays.copyOf(elementData, newCapacity);
	}

Vector与ArrayList的区别

同ArrayList,唯一区别是Vector是线程安全的,ArrayList是线程安全的,内部扩容方式不一样。

Vector扩容程度与扩容因子有关。初始化Vector的时候如果初始化扩容因子,则首先选择扩容当前容量+扩容因子;如果没有初始化扩容因子,则选择扩容当前容量的2倍。核心代码如下:

private void grow(int minCapacity) {
	    // overflow-conscious code
	    int oldCapacity = elementData.length;
	    int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
	                                     capacityIncrement : oldCapacity);
	    if (newCapacity - minCapacity < 0)
	        newCapacity = minCapacity;
	    if (newCapacity - MAX_ARRAY_SIZE > 0)
	        newCapacity = hugeCapacity(minCapacity);
	    elementData = Arrays.copyOf(elementData, newCapacity);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值