zoukankan      html  css  js  c++  java
  • Java集合-06Vector源码解析及使用实例

    Vector简介

    Vector (矢量类)实现了动态数组的功能,如同数组,它可以通过角标访问数据,
    不过Vector被创建后在添加或移除时候能适应性的增加或者减少。继承AbstractCollection类,实现了List、RandomAccess、Cloneable、Serialization接口

    构造方法

    1. public Vector()
    2. public Vector(int initialCapacity)
    3. public Vector(int initialCapacity, int capacityIncrement)

    Vector包含三个构造函数,第一种默认构造函数,此时初始容量为10,
    第二种指定容量大小的构造函数,此时capacityIncrement等于0,扩容时候成倍增加,
    第三种指定容量大小和增量,扩容时候增加capacityIncrement大小

    重要属性

    1. protected Object[] elementData;//用于保存Vector数据的数组
    2. protected int elementCount;//Vector中数据个数
    3. protected int capacityIncrement;//Vector容器的增量

    Vector的方法和ArrayList类似,不过它的增删改查方法前都添加synchronized修饰,保证线程安全,也就是说Vector是线程同步的

    Vector源码分析

    public synchronized boolean add(E e) {
            modCount++;//fail-fast判断机制
            ensureCapacityHelper(elementCount + 1);//确保Vector容量足够,跳转到下一个方法
            elementData[elementCount++] = e;//对应位置复制
            return true;//返回布尔类型
        }
    

    fail-fast(快速失败)机制见上一篇博客

    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);//如果增量大于0,扩容后的大小为原来容量与增量的和,否则扩容为原来容量的一倍
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
    
    public synchronized E remove(int index) {
            modCount++;//fail-fast
            if (index >= elementCount)
                throw new ArrayIndexOutOfBoundsException(index);//数组角标过界异常
            E oldValue = elementData(index);//获取当前角标位置的元素数据
    
            int numMoved = elementCount - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,numMoved);//复制数组(复制源数组,复制源数组开始位置,复制数组,复制数组开始位置,复制长度)
            elementData[--elementCount] = null; // Let gc do its work
    
            return oldValue;
        }
    

    Vector遍历方式

    Vector vect = new Vector<>(10);

    • 迭代器遍历

      •   Iterator<String> iterator = vect.iterator();
                  while (iterator.hasNext()){
                      iterator.next();
          }
        
    • forEach遍历

      •   for (String s : vect) {
          
          }
        
    • RandomAccess遍历

      •   for (int i = 0;i<vect.size(); i++){
                      vect.get(i);
          }
        
    • Enumeration遍历

      •   Enumeration<String> elements = vect.elements();
                  while (elements.hasMoreElements()){
                      elements.nextElement();
          }
        

    Vector使用实例

    使用实例代码在Github

  • 相关阅读:
    QT 制作串口调试小助手----(小白篇)
    外卖自提柜设备端主控
    C++的六个函数
    后台业务系统设计流程的6个步骤
    C#中如何将图片添加为程序的资源
    提供Chrome浏览器插件网站
    是时候弃用ibSass和node-sass而采用Dart Sass了!
    vue引入element-ui插件 “export ‘default‘ (imported as ‘Vue‘) was not found in ‘vue‘
    [转]Vue3.0和Vue2.0的区别
    vue-cli@4搭建 vue + element-ui 项目实操
  • 原文地址:https://www.cnblogs.com/JzedyBlogs/p/10140642.html
Copyright © 2011-2022 走看看