常见集合类(ArrayList)

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。常见集合类(ArrayList),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

ArrayList

  • 主要成员变量:
transient Object[] elementData; //底层是一个数组结构,用来保存元素
private static final int DEFAULT_CAPACITY = 10;//默认初始化容量大小为10

构造方法

在这里插入图片描述
主要构造方法: public ArrayList(int initialCapacity)

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);
        }
    }

add方法

add(E e)&add(int index, E element)两个重载

在这里插入图片描述

public boolean add(E e) {
        //1. 确定list的空间大小
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //2. 在数组从左向右第一个为null的位置填充e
        elementData[size++] = e;
        return true;
    }

ensureCapacityInternal(int minCapacity)方法:相当于grow方法的一个前奏吧

private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            //最小的容量为10
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        ensureExplicitCapacity(minCapacity);
    }
    
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        // overflow-conscious code
        //如果最大容量已经达到,对数组进行扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

grow方法:就是用来对底层的数组结构进行扩容

 private void grow(int minCapacity) {
        // 旧的数组长度
        int oldCapacity = elementData.length;
        //新的数组长度 = 旧的数组长度 + 旧的数组长度*0.5
        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:
        //把原来的数组里面的元素复制到新数组里面,并且把当前list的元素指针指向新的数组
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

get方法

这个方法就是返回执行下标位置的元素

public E get(int index) {
	    //检查数组下标是否越界
        rangeCheck(index);       
        return elementData(index);
    }

remove方法

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; // clear to let GC do its work

        return oldValue;
    }

    /**
     * Removes the first occurrence of the specified element from this list,
     * if it is present.  If the list does not contain the element, it is
     * unchanged.  More formally, removes the element with the lowest index
     * <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
     * (if such an element exists).  Returns <tt>true</tt> if this list
     * contained the specified element (or equivalently, if this list
     * changed as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
     */
    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }

    /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

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

这个方法主要就是完成数组的复制

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/202527.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!