Vector底层实现(常用方法)

其实Vector底层和ArrayList底层差不多,就是多了synchronized,大部分还是一样的

import java.util.Arrays;

public class VectorTest<E> implements java.io.Serializable {
    Object[] elementData;
    int elementCount;
    int capacityIncrement;
    int MAX_ARRAY_SIZE=Integer.MAX_VALUE-8;

    public VectorTest(int initialCapacity,int capacityIncrement) {
        if (initialCapacity<0)
            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
        this.elementData=new Object[initialCapacity];
        this.capacityIncrement=capacityIncrement;
    }

    public VectorTest(int initialCapacity) {
        //调用上面那个构造方法
        this(initialCapacity,0);
    }

    public VectorTest() {
        this(10);
    }

    public synchronized boolean add(E e) {
        ensureCapacityHelper(elementCount+1);
        elementData[elementCount++]=e;
        return true;
    }


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

    public synchronized E set(int index,E element) {
        if(index>=elementCount)
            throw new ArrayIndexOutOfBoundsException();

        E oldVal=(E)elementData[index];
        elementData[index]=element;
        return oldVal;
    }

    public boolean remove(Object o) {
        return removeElement(o);
    }

    public synchronized boolean removeElement(Object o) {
        //获取到元素的下标
        int i=indexOf(o,0);
        if(i>=0) {
            //将该元素后面的元素都往前移,使用arraycopy方法
            removeElementAt(i);
            return true;
        }
        return false;
    }

    public synchronized void removeElementAt(int index) {
        if(index>=elementCount||index<0)
            throw new ArrayIndexOutOfBoundsException();
        //这些元素都是需要移动的
        int j=elementCount-index-1;

        if(j>0) {
            System.arraycopy(elementData,index+1,elementData,index,j);
        }
        elementCount--;
        elementData[elementCount]=null;
    }

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

    public void ensureCapacityHelper(int minCapacity) {
        //如果数组最小要求长度大于数组现在的长度,就需要扩容
        if(minCapacity-elementData.length>0) {
            grow(minCapacity);
        }
    }

    public void grow(int minCapacity) {
        int oldCapacity=elementData.length;
        //要根据设置的每次扩容的大小来进行扩容,如果设置的步长大于0,则就加上步长,否则,就是扩大二倍
        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);
    }


    public int hugeCapacity(int minCapacity) {
        if(minCapacity<0)
            throw new OutOfMemoryError();

        return (minCapacity>MAX_ARRAY_SIZE?Integer.MAX_VALUE:MAX_ARRAY_SIZE);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值