美文网首页
ArrayList 跟Vector的区别

ArrayList 跟Vector的区别

作者: leenpong | 来源:发表于2018-12-09 15:50 被阅读0次

相同点,都是继承AbstractList,而且内部都是用一个 protected Object[] elementData的数组用来保存数据,当vector默认是10个长度。而Arraylist是空。

ArrayList:

当list满的时候,扩容如下:

private static int newCapacity(int currentCapacity) {
        int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ?
                MIN_CAPACITY_INCREMENT : currentCapacity >> 1);
        return currentCapacity + increment;
    }

其中 private static final int MIN_CAPACITY_INCREMENT = 12;
也就是说,每次扩容,是在当前的容量加上6或者当前容量的一半。

Vector:
private void growByOne() {
        int adding = 0;
        if (capacityIncrement <= 0) {
            if ((adding = elementData.length) == 0) {
                adding = 1;
            }
        } else {
            adding = capacityIncrement;
        }

        E[] newData = newElementArray(elementData.length + adding);
        System.arraycopy(elementData, 0, newData, 0, elementCount);
        elementData = newData;
    }

也就是说,vector每次默认是增加1.当然如果有给capacityIncrement设置值得话,每次就会增加相应的个数。

相关文章

网友评论

      本文标题:ArrayList 跟Vector的区别

      本文链接:https://www.haomeiwen.com/subject/zlqkhqtx.html