精华内容
下载资源
问答
  • ArrayList扩容机制

    2021-02-19 00:23:18
    ArrayList复习ArrayList扩容机制构造函数扩容流程ensureCapacity函数 ArrayList扩容机制 构造函数 /** * 指定初始容量 */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this....

    ArrayList扩容机制

    构造函数

    /**
     * 指定初始容量 
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
    
    /**
     * 不指定初始容量,使用默认的容量10(实际上是0,添加第一个元素之后扩容为10)
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    
    /**
     * 使用Collection作为参数,创建一个新的复制List
     *
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }
    

    扩容流程

    假设使用了无参的构造方法,添加第一个元素之后扩容

    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
    

    调用了ensureCapacityInternal方法,

    private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }
    

    又调用了ensureExplicitCapacity方法,minCapacity为1,elementData.length为0

    private void ensureExplicitCapacity(int minCapacity) {
        //修改次数增加
        modCount++;
    
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    

    调用grow方法进行扩容

    private void grow(int minCapacity) {
        // overflow-conscious code
        //旧容量
        int oldCapacity = elementData.length;
        //新容量使用1.5倍进行试探
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        //1.5倍不够就扩容为minCapacity
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        //如果扩容超出了最大容量,则进行修正
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    

    hugeCapacity函数如下

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

    MAX_ARRAY_SIZE最大容量为Integer.MAX_VALUE - 8(如下),官方解释有些虚拟机需要在array设置header words,超过这个数可能报异常

    /**
     * The maximum size of array to allocate.
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    

    执行完成后就可以赋值了

    ensureCapacity函数

    这个函数是给用户调用的,手动扩容,避免多次增量扩容,影响性能

    /**
     * Increases the capacity of this <tt>ArrayList</tt> instance, if
     * necessary, to ensure that it can hold at least the number of elements
     * specified by the minimum capacity argument.
     *
     * @param   minCapacity   the desired minimum capacity
     */
    public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;
    
        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }
    
    展开全文
  • arraylist扩容机制

    2021-04-20 14:13:07
    如果没有向集合中添加任何元素时, 容量为0 size为0 添加第一个元素的时候,容量为10 才扩容 容量第一次为10 后面是原来的1.5倍

    如果没有向集合中添加任何元素时, 容量为0 size为0
    添加第一个元素的时候,容量为10 才扩容 容量第一次为10 后面是原来的1.5倍

    展开全文
  • Arraylist扩容机制

    2021-02-27 19:31:50
    public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } ...

    构造方法:

    指定容量

     public ArrayList(int initialCapacity) {
            if (initialCapacity > 0) {
                this.elementData = new Object[initialCapacity];
            } else if (initialCapacity == 0) {
                this.elementData = EMPTY_ELEMENTDATA;
            } else {
                throw new IllegalArgumentException("Illegal Capacity: "+
                                                   initialCapacity);
            }
        }
    

    不指定容量

     public ArrayList() {
            this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
        }
    

    add方法:

     public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }
    
     private void ensureCapacityInternal(int minCapacity) {
            ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
        }
    
      private void ensureExplicitCapacity(int minCapacity) {
            modCount++;
    
            // 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 + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
    
    

    综上,ArrayList相当于在没指定initialCapacity时就是会使用延迟分配对象数组空间,当第一次插入元素时才分配10(默认)个对象空间(在插入得时候有个calculateCapacity方法)。假如有20个数据需要添加,那么会分别在第一次的时候,将ArrayList的容量变为10 (如下图一);之后扩容会按照1.5倍增长。也就是当添加第11个数据的时候,Arraylist继续扩容变为10*1.5=15(如下图二);当添加第16个数据时,继续扩容变为15 * 1.5 =22个(如下图四)。:

    参考:https://blog.csdn.net/u010176014/article/details/52073339

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,677
精华内容 670
关键字:

arraylist扩容机制