zoukankan      html  css  js  c++  java
  • jdk源码——ArrayList

    ArrayList的初始化,我们可以指定大小也可以不指定大小,默认大小是10,

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

        /**
         * Constructs an empty list with an initial capacity of ten.
         */
        public ArrayList() {
            this(10);
        }

    这个方法是直接将一个集合作为ArrayList的元素,此时elementData即为集合c转为的数组,size即为elementData的长度。这里size是ArrayList的一个int型私有变量,用于记录该list集合中当前元素的数量,注意不是容量。

     public ArrayList(Collection<? extends E> c) {
            elementData = c.toArray();
            size = elementData.length;
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        }

    扩容操作,源码如下,标红的部分即为,扩容的主要操作,新的容量为原来容量的1.5倍。扩容后还要对newCapacity进行判断如果仍然小于minCapacity,直接让newCapacity等于minCapacity,而不是扩容为原来的1.5倍。然后还要进一步进行判断,即当前的新容量是否超过最大容量如果超过,则调用hugeCapacity方法,传进去的是minCapacity,即新增元素后需要的最小容量:

    我们观察ensureCapacityInternal方法里面有个modCount变量,它代表list对象被修改的次数,每次操作都会增加1,迭代类里有一个成员变量expectedModCount,它的值为创建Itr对象的时候List的modCount值。用此变量来检验在迭代过程中List对象是否被修改了,如果被修改了则抛出java.util.ConcurrentModificationException异常。在每次调用Itr对象的next()方法的时候都会调用checkForComodification()方法进行一次检验,checkForComodification()方法中做的工作就是比较expectedModCount 和modCount的值是否相等,如果不相等,就认为还有其他对象正在对当前的List进行操作,那个就会抛出ConcurrentModificationException异常。

    public void ensureCapacity(int minCapacity) {
            if (minCapacity > 0)
                ensureCapacityInternal(minCapacity);
        }

        private void ensureCapacityInternal(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);
        }

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

    Size()获取集合的大小

     public int size() {
            return size;
        }

    判断集合是否为空
    public boolean isEmpty() {
            return size == 0;
        }

    判断集合是否包含传入元素,从源码中可以看出,先对传入的元素进行判空操作,若不为空则遍历当前集合,去匹配传入元素,返回匹配到的下标,若最后匹配不到返回-1;

    public boolean contains(Object o) {
            return indexOf(o) >= 0;
        }

    public int indexOf(Object o) {
            if (o == null) {
                for (int i = 0; i < size; i++)
                    if (elementData[i]==null)
                        return i;
            } else {
                for (int i = 0; i < size; i++)
                    if (o.equals(elementData[i]))
                        return i;
            }
            return -1;
        }

    get、set操作

     public E get(int index) {
            rangeCheck(index);

            return elementData(index);
        }

    public E set(int index, E element) {
            rangeCheck(index);

            E oldValue = elementData(index);
            elementData[index] = element;
            return oldValue;
        }

    add操作,ensureCapacityInternal方法里面传入的是size+1,确保增加后的容量,

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

    remevo操作

     public E remove(int index) {
            rangeCheck(index);//检查下标是否越界

            modCount++;
            E oldValue = elementData(index);

            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // Let gc do its work

            return oldValue;
        }

  • 相关阅读:
    Linux入门
    Python接入支付宝进行PC端支付
    DRF之注册器响应器分页器
    Sencha Touch 实战开发培训 视频教程 第二期 第七节
    Sencha Touch 实战开发培训 视频教程 第二期 第六节
    Sencha Touch 实战开发培训 视频教程 第二期 第五节
    sencha touch 百度地图扩展(2014-6-24)(废弃 仅参考)
    Sencha Touch 实战开发培训 视频教程 第二期 第四节
    Sencha Touch 实战开发培训 视频教程 第二期 第三节
    sencha touch 扩展官方NavigationView 灵活添加按钮组,导航栏,自由隐藏返回按钮(2014-5-15)
  • 原文地址:https://www.cnblogs.com/wanglingdeboke/p/9662413.html
Copyright © 2011-2022 走看看