• ArrayList源码分析


    ArrayList是一种最常用的集合类,底层数据结构是数组,提供动态扩展数组长度的特性,允许元素的值为null。ArrayList是一种非线程安全的集合类,若要在多线程的环境,需要注意同步问题,也可以使用Collections.synchronizedList()方法保证线程安全问题。

    继承关系

    ArrayList

    构造方法

    默认构造方法,创建一个空的数组对象

        public ArrayList() {
            super();
            this.elementData = EMPTY_ELEMENTDATA;
        }
        private static final Object[] EMPTY_ELEMENTDATA = {};
    

    创建指定大小的数组对象

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

    根据一个集合对象创建ArrayList

        public ArrayList(Collection<? extends E> c) {
            elementData = c.toArray();
            size = elementData.length;
    		// c.toArray()的返回结果可能并不是Object数组对象
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        }
    

    主要方法

    add

        public boolean add(E e) {
            ensureCapacityInternal(size + 1);
            elementData[size++] = e;
            return true;
        }
    	// 确定数组大小
        private void ensureCapacityInternal(int minCapacity) {
            if (elementData == EMPTY_ELEMENTDATA) {
                minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }
    
            ensureExplicitCapacity(minCapacity);
        }
    	// 数组扩容
        private void ensureExplicitCapacity(int minCapacity) {
    		// 记录ArrayList结构被修改的次数
            modCount++;
    
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
        private void grow(int minCapacity) {
            int oldCapacity = elementData.length;
    		// 当前数组大小的1.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:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
    
    	
       public void add(int index, E element) {
    		// 数组越界检测	
           rangeCheckForAdd(index);
    
           ensureCapacityInternal(size + 1);
    		// 将元素插在指定的位置
           System.arraycopy(elementData, index, elementData, index + 1,
                   size - index);
           elementData[index] = element;
           size++;
       }
    

    get

    返回指定位置的元素

        public E get(int index) {
            rangeCheck(index);
    
            return elementData(index);
        }
    

    set

    将原数组中的元素返回,并将新元素插入

        public E set(int index, E element) {
            rangeCheck(index);
    
            E oldValue = elementData(index);
            elementData[index] = element;
            return oldValue;
        }
    

    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;
        }
        
        // 移除指定对象
        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;
        }
    

    本文由博客一文多发平台 OpenWrite 发布!

  • 相关阅读:
    常用控件(1)—RadioGroup、CheckBox、Toast
    调用系统剪切板并传输到OtherActivity
    Linux 文件类型
    Handler应用3 — 与Activity不同一线程
    linux下tftp(解决Loading问题 transfer timed out)
    c/c++ 中const的作用
    C++ 初步知识
    类外定义成员函数实例
    ubuntu上Samba服务器配置
    Android全面开发——问题汇总
  • 原文地址:https://www.cnblogs.com/thisismartin/p/11993822.html
Copyright © 2020-2023  润新知