浅谈Vector

作者: 小鱼嘻嘻 | 来源:发表于2017-10-22 12:15 被阅读23次
Vector结构图
Vector.png
Vector主要方法
  • public synchronized boolean add(E e);
  • public synchronized E set(int index, E element) ;
  • public synchronized E get(int index);
  • public boolean remove(Object o);
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其他特性介绍
  • 首先,vector是用数组实现的,所以ArrayList的数组特性vector也是有的,其次,vector是有synchronized锁机制的,所以他是线程安全的,最后vector也会抛出ConcurrentModificationException这个异常。这个我有点不明白,为啥线程安全了还会抛出这个异常呢?每次当有线程做删除操作的时候,就不应该有线程可以去get或者set了呀! 这个问题TODO处理,希望有网友可以解决我的疑惑。
  • vector默认的初始值是10,private static final int DEFAULT_CAPACITY = 10;每次扩容的容量为: int newCapacity = oldCapacity + (oldCapacity >> 1);约1.5倍

相关文章

  • 浅谈Vector

    Vector结构图 Vector主要方法 public synchronized boolean add(E e)...

  • 浅谈vector

    简介 std::vector (向量): C++中的一种数据结构, 是封装动态数组的序列容器。内部元素是连续存储的...

  • c++常用数据结构

    问题:vector与数组的区别? 1、vector vector v;//创建vector v....

  • 浅谈java中的Vector,ArrayList,LinkedL

    第一,简介 这三者都是实现集合框架中的List,也就是所谓的有序集合,因此具体功能也比较近似,比如都提供按照位置进...

  • 指针

    ①动态指针问题. vector * pvt = new vector [100]; vector * tt; tt...

  • 2021-12-01 opencv findContours a

    std::vector contours; std::vector hi...

  • 13.C++ vector 操作

    vector初始化 vector大小 数组方式操作vector 迭代器方式操作vector 反向迭代器操作vect...

  • c++零散知识

    1、vector vector对象内存增长原理,当向vector添加新元素而vector没有足够的内存保存新元素时...

  • vector

    一、vector介绍:可边长数组1、vector的定义:vector name;注:vecto...

  • Collection-Vector

    jdk版本:1.8.0_77参考文档:jdk 1.8 docs Vector类图 Vector特点 Vector对...

网友评论

    本文标题:浅谈Vector

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