zoukankan      html  css  js  c++  java
  • Vector数据结构(jdk8)

    看了下Vector的数据结构,简要记录下。

    成员变量

        //放数据的
        protected Object[] elementData;
        //数据数量
        protected int elementCount;
        //要扩充的容量增长
        protected int capacityIncrement;
            
    

    添加方法

        public synchronized boolean add(E e) {
            //操作数+1
            modCount++;
            //确认容量够不够
            ensureCapacityHelper(elementCount + 1);
            //赋值
            elementData[elementCount++] = e;
            return true;
        }
    
         private void ensureCapacityHelper(int minCapacity) {
            //溢出要扩容
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
    
        private void grow(int minCapacity) {
            //旧容量
            int oldCapacity = elementData.length;
            //扩容后的长度,默认是2倍,如果capacityIncrement不为空就以它为增量
            int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                             capacityIncrement : oldCapacity);
            //取max(newCapacity,minCapacity)为新容量
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            //新容量溢出的处理
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            //复制数组
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
        
        //返回 处理过溢出情况的minCapacity,
        private static int hugeCapacity(int minCapacity) {
            if (minCapacity < 0) // overflow
                throw new OutOfMemoryError();
            return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
        }
    
    
  • 相关阅读:
    每天读一下,你就会改变
    C++ 反转字符串(原创)
    C++ string学习
    18种常见室内花卉的功效 (转自网络)
    UML建模教程
    孙鑫视频VC++深入详解学习笔记
    visual C++ 6.0开发工具与调试
    C++ typeid typename使用
    C++模板学习
    Working classes Code complete reading notes(6)
  • 原文地址:https://www.cnblogs.com/june777/p/11731974.html
Copyright © 2011-2022 走看看