zoukankan      html  css  js  c++  java
  • ArrayList源码分析

    1. 源码分析

      

    public ArrayList() {.   //默认构造函数,若初始值指定为null则将赋值为一个空的数组
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    
    public ArrayList(int initialCapacity) {      //指定初始值
        if (initialCapacity > 0) {               //若大于零则初始化一个Object数组
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {        //赋值为一个空的数组
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
    
    
    public boolean add(E e) {      //添加元素
        modCount++;                //modCount表示容器被改变的次数
        add(e, elementData, size); 
        return true;
    }
    
    private void add(E e, Object[] elementData, int s) {
        if (s == elementData.length)           //如果说数组已满则会进行扩容操作
            elementData = grow();            
        elementData[s] = e;                //将元素插入到当前位置
        size = s + 1;
    }
    
    private Object[] grow() {
        return grow(size + 1);
    }
    
    private Object[] grow(int minCapacity) {          //将元素复制到新的数组
        return elementData = Arrays.copyOf(elementData,
                                           newCapacity(minCapacity));
    }
    
    private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;        
        int newCapacity = oldCapacity + (oldCapacity >> 1);    //扩容后的容量1.5倍
        if (newCapacity - minCapacity <= 0) {                //扩容后的小于最小要求的容量
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)    //没有指定初始容量
                return Math.max(DEFAULT_CAPACITY, minCapacity);  
            if (minCapacity < 0) // overflow  
                throw new OutOfMemoryError();
            return minCapacity;
        }
        return (newCapacity - MAX_ARRAY_SIZE <= 0)
            ? newCapacity
            : hugeCapacity(minCapacity);
    }
    
    public E remove(int index) {
        Objects.checkIndex(index, size);   //检测要移除的是否超过容器长度
        final Object[] es = elementData;    
    
        @SuppressWarnings("unchecked") E oldValue = (E) es[index];
        fastRemove(es, index);
    
        return oldValue;
    }
    
    private void fastRemove(Object[] es, int i) {
        modCount++;                    //modCount为容器改变的次数
        final int newSize;            //新的容器大小为size-1
        if ((newSize = size - 1) > i)        
            System.arraycopy(es, i + 1, es, i, newSize - i);    //将要删除的元素直接覆盖掉
        es[size = newSize] = null;   //最后一个元素则直接设置为null
    }
    
    
    
    
    //一些成员变量
    /**
    * Default initial capacity.
    */
    private static final int DEFAULT_CAPACITY = 10;   //默认的长度,方初始化长度为null是在扩容是会指定                                        
                                                        为10
    
    /**
    * Shared empty array instance used for empty instances.
    */
    private static final Object[] EMPTY_ELEMENTDATA = {};   //在初始化时将长度指定为0
    
    /**
    * Shared empty array instance used for default sized empty instances. We
    * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
    * first element is added.
    */
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};   //不指定初始化容量时的大小
    
    /**
    * The array buffer into which the elements of the ArrayList are stored.
    * The capacity of the ArrayList is the length of this array buffer. Any
    * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
    * will be expanded to DEFAULT_CAPACITY when the first element is added.
    */
    transient Object[] elementData; // non-private to simplify nested class access //用来存储容器的,transient不会再序列化时被写入
            
    
    /**
    * The size of the ArrayList (the number of elements it contains).
    *
    * @serial
    */
    private int size;        //存储的元素的数量(不是elementData的长度)
    
    /**
    * Constructs an empty list with the specified initial capacity.
    *
    * @param  initialCapacity  the initial capacity of the list
    * @throws IllegalArgumentException if the specified initial capacity
    *         is negative
    */

    2. 为什么要使用transient来修饰elementData?

      由于elementData是一个可缓冲的数组,在一般情况下该数组可能不为满(因为每次数组满后都会进行1.5被扩容),因此不会直接使用该数组进行序列化。

    3. ArrayList如何进行序列化?(关于序列化的具体细节可看另一篇博客)

      arrayList通过继承Serializable接口,重写了readObject(),writeObject()方法。

    private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException{
            // Write out element count, and any hidden stuff
            int expectedModCount = modCount;
            s.defaultWriteObject();
    
            // Write out size as capacity for behavioural compatibility with clone()
            s.writeInt(size);
    
            // Write out all elements in the proper order.
            for (int i=0; i<size; i++) {       //size为实际元素的数量,遍历数组将不为空的元素进行序列化
                s.writeObject(elementData[i]);
            }
    
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
        }
  • 相关阅读:
    C# -- HttpWebRequest 和 HttpWebResponse 的使用
    C# -- Lambda 表达式的使用
    ASP.NET -- WebForm -- HttpRequest类的方法和属性
    ASP.NET -- WebForm -- HttpResponse 类的方法和属性
    C# -- 索引器、枚举类型
    C#设计模式之七桥接模式(Bridge Pattern)【结构型】
    C#设计模式之六适配器模式(Adapter Pattern)【结构型】
    C#设计模式之五原型模式(Prototype Pattern)【创建型】
    C#设计模式之四建造者模式(Builder Pattern)【创建型】
    C#设计模式之三抽象工厂模式(AbstractFactory)【创建型】
  • 原文地址:https://www.cnblogs.com/liwangcai/p/11884874.html
Copyright © 2011-2022 走看看