zoukankan      html  css  js  c++  java
  • 大杂烩 -- ArrayList的动态增长 源码分析

    基础大杂烩 -- 目录

    -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    问题:当ArrayList中放入的元素一直增加会如何增长数组长度???

    Class : ArrayList

    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    
        /**
         * 默认容量值
         */
        private static final int DEFAULT_CAPACITY = 10;
    
        /**
         * 默认容量为空的数组
         */
        private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    
        /**
         * 用来存储元素
         */
        transient Object[] elementData;
    
        /**
         * 数组大小
         *
         * @serial
         */
        private int size;
    
        /**
         * 添加元素
         * 
         * @param e
         * @return
         */
        public boolean add(E e) {
            ensureCapacityInternal(size + 1);
            elementData[size++] = e;
            return true;
        }
    
        /**
         * 确保容量动态增长
         * 
         * @param minCapacity
         */
        private void ensureCapacityInternal(int minCapacity) {
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
                minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }
    
            ensureExplicitCapacity(minCapacity);
        }
    
        /**
         * 确定新的容量
         * 
         * @param minCapacity
         */
        private void ensureExplicitCapacity(int minCapacity) {
            modCount++;
    
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
    
        /**
         * 数组最大大小
         */
        private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    
        /**
         * 数组容量动态增长
         * 
         */
        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);
        }
    
        /**
         * 计算数组最终容量
         * 
         * @param minCapacity
         * @return
         */
        private static int hugeCapacity(int minCapacity) {
            if (minCapacity < 0) // overflow
                throw new OutOfMemoryError();
            return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
        }
    }

    Class : Arrays

        @SuppressWarnings("unchecked")
        public static <T> T[] copyOf(T[] original, int newLength) {
            return (T[]) copyOf(original, newLength, original.getClass());
        }
        public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
            @SuppressWarnings("unchecked")
            T[] copy = ((Object)newType == (Object)Object[].class)
                ? (T[]) new Object[newLength]
                : (T[]) Array.newInstance(newType.getComponentType(), newLength);
            System.arraycopy(original, 0, copy, 0,
                             Math.min(original.length, newLength));
            return copy;
        }

    Class : System

       public static native void arraycopy(Object src,  int  srcPos,
                                            Object dest, int destPos,
                                            int length);

    总的来说依旧是调用System类中native方法重新创造一个数组。

    啦啦啦

  • 相关阅读:
    如何把pdf文档转化为word
    Win7系统中wmiprvse.exe占用CPU高如何解决
    Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
    SQL Server新增用户并控制访问权限设置。
    vs2013 中已经添加了引用,编译还是提示没有添加引用
    vue-cli中引入jquery方法
    vue-vuex安装
    vue2.0项目结构和打包发布
    从(0)新开始vue2.0【安装】
    js判断网络连接情况:navigator.onLine
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/7010602.html
Copyright © 2011-2022 走看看