Java基础程序员

浅谈Vector

2017-10-22  本文已影响23人  小鱼嘻嘻
Vector结构图
Vector.png
Vector主要方法
Vector解读主要方法

来看一下public synchronized boolean add(E e)源码

//首先,需要说明的是这个synchronized,这是加锁操作,保证线程安全
public synchronized boolean add(E e) {
        modCount++;
        //确保容量
        ensureCapacityHelper(elementCount + 1);
        //添加元素,数量加一
        elementData[elementCount++] = e;
        return true;
    }
//这些操作和ArrayList的一致,可以参考ArrayList
 private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
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);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

来看一下public synchronized E set(int index, E element) 源码

public synchronized E set(int index, E element) {
        //判断index位置是否合理
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
        //获取index位置老元素
        E oldValue = elementData(index);
       //替换index位置为新元素 
       elementData[index] = element;
        return oldValue;
    }

来看一下public synchronized E get(int index)源码

public synchronized E get(int index) {
        //判断index位置是否合理
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
          //获取index位置元素
        return elementData(index);
    }

来看一下public boolean remove(Object o)源码

//首先很奇怪的是这个方法没有加锁
  public boolean remove(Object o) {
        //来看一下真正的实现
        return removeElement(o);
    }
//其实还是加锁了
public synchronized boolean removeElement(Object obj) {
        //修改modCount
        modCount++;
        //找到待删除元素的位置
        int i = indexOf(obj);
        if (i >= 0) {
            //真正的删除元素
            removeElementAt(i);
            return true;
        }
        return false;
    }
//删除index
public synchronized void removeElementAt(int index) {
        modCount++;
         //判断删除位置是否合理
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        else if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        //确定需要移动元素个数
        int j = elementCount - index - 1;
        if (j > 0) {
            //用index后一个元素覆盖index,依次移动,一共移动j个元素
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        //总数减一,最后一个元素置空
        elementCount--;
        elementData[elementCount] = null; /* to let gc do its work */
    }

Vector遍历介绍

常用的三种遍历方式:

       //one  foreach  遍历
        for (Object o : list) {
            System.out.println(o);
        }
        // two 随机访问
        for (int i = 0; i <list.size(); i++) {
            System.out.println(list.get(i));
        }
        //three 迭代器的遍历
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
Vector其他特性介绍
上一篇 下一篇

猜你喜欢

热点阅读