ArrayList源码分析

栏目: 编程工具 · 发布时间: 4年前

内容简介:ArrayList是一种最常用的集合类,底层数据结构是数组,提供动态扩展数组长度的特性,允许元素的值为null。ArrayList是一种非线程安全的集合类,若要在多线程的环境,需要注意同步问题,也可以使用Collections.synchronizedList()方法保证线程安全问题。默认构造方法,创建一个空的数组对象创建指定大小的数组对象

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;
    }
复制代码

更多内容请关注我的个人主页


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

Lincoln Stein、Doug MacEachern / O'Reilly Media, Inc. / 1999-03 / USD 39.95

Apache is the most popular Web server on the Internet because it is free, reliable, and extensible. The availability of the source code and the modular design of Apache makes it possible to extend Web......一起来看看 《Writing Apache Modules with Perl and C》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器