Java中ArrayList源码分析

一、简介

ArrayList是一个数组队列,相当于动态数组。每个ArrayList实例都有自己的容量,该容量至少和所存储数据的个数一样大小,在每次添加数据时,它会使用ensureCapacity()保证容量能容纳所有数据。

1.1、ArrayList 的继承与实现接口

ArrayList继承于AbstractList,实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。

public class  ArrayList<E> extends AbstractList<E>         implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  • ArrayList 继承了AbstractList,实现了List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
  • ArrayList 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在ArrayList中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。
  • ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。不过它实现的是对ArrayList 实例的浅拷贝。
  • ArrayList 实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。

1.2、ArrayList 与Collection集合的关系

1.3、ArrayList 与Vector的区别

ArrayList 与Vector大致等同,唯一的区别就是ArrayList的操作是线程不安全的,然而Vector是线程安全的。所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector或者CopyOnWriteArrayList。

二、源码分析

对于ArrayList而言,它实现List接口、底层使用数组保存所有元素。其操作基本上是对数组的操作。

2.1、成员变量

private static final long serialVersionUID = 8683452581122892189L;    // 使用serialVersionUID验证版本一致性private static final int DEFAULT_CAPACITY = 10;    // 容量的初始大小private static final Object[] EMPTY_ELEMENTDATA = {};    // Shared empty array instance used for empty instances. private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};    // Shared empty array instance used for default sized empty instances.// ArrayList数据的存储之地transient Object[] elementData;    // 存储ArrayList中元素的数组// ArrayList存储数据的个数private int size;     

2.2、构造方法

ArrayList提供了三种方式的构造器方法:

1)通过传入的initialCapacity大小构造ArrayList

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

2)使用初始容量值构造ArrayList

public  ArrayList() {    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}

3)通过传入的指定类集构造ArrayList

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

    } else    {        // replace with empty array.        this.elementData = EMPTY_ELEMENTDATA;    }}

 

2.3、增加元素操作

1)往数组尾部添加元素

public boolean add(E e){    // 1、确保容量大小    ensureCapacityInternal(size + 1); // Increments modCount!!    // 2、尾部添加元素    elementData[size++] = e;    return true;}

2)在指定位置添加元素

public void add(int index, E element){    // 1、检验索引    rangeCheckForAdd(index);    // 2、确保容量    ensureCapacityInternal(size + 1); // Increments modCount!!    // 3、将数据后移    System.arraycopy(elementData, index, elementData, index + 1,    size - index);    // 4、添加元素    elementData[index] = element;    // 5、数组元素个数加1    size++;}
==>System.arraycopy(src, srcPos, dest, destPos, length);

3)往数组尾部添加某类集合中所有元素,针对泛型

public boolean addAll(Collection<? extends E> c){    // 1、暂存集合c中数据    Object[] a = c.toArray();    int numNew = a.length;    // 2、确保容量    ensureCapacityInternal(size + numNew); // Increments modCount    // 3、尾部添加数据    System.arraycopy(a, 0, elementData, size, numNew);    // 4、数组元素个数更新    size += numNew;    return numNew != 0;}

注意:The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

4)在指定位置添加某类集合中所有元素,针对泛型

public boolean addAll(int index, Collection<? extends E> c){    // 1、检验索引    rangeCheckForAdd(index);    // 2、暂存数据     Object[] a = c.toArray();    int numNew = a.length;    // 3、确保容量    ensureCapacityInternal(size + numNew); // Increments modCount    int numMoved = size - index;    // 4、数据后移    if (numMoved > 0)        System.arraycopy(elementData, index, elementData, index + numNew,        numMoved);    // 5、添加元素    System.arraycopy(a, 0, elementData, index, numNew);    // 6、数组元素个数更新    size += numNew;    return numNew != 0;}

 

2.4、删除元素操作

1)删除ArrayList中第一个符合条件的元素

public boolean remove(Object o){    // 移除值为null的元素    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 void fastRemove(int index){    // 1、修改的次数更新    modCount++;    int numMoved = size - index - 1;    // 2、数据前移    if (numMoved > 0)        System.arraycopy(elementData, index + 1, elementData, index,        numMoved);    elementData[--size] = null; // clear to let GC do its work}

2)删除ArrayList中指定位置上的元素

public E remove(int index){    // 1、检验索引    rangeCheck(index);    modCount++;    E oldValue = elementData(index);    int numMoved = size - index - 1;    // 2、数据前移    if (numMoved > 0)        System.arraycopy(elementData, index + 1, elementData, index,        numMoved);    elementData[--size] = null; // clear to let GC do its work    return oldValue;}

3)删除ArrayList中所包含传入集合类中的所有元素,针对泛型

public boolean removeAll(Collection<?> c){    Objects.requireNonNull(c);    return batchRemove(c, false);}

4)删除某个范围的数据。

protected void removeRange(int fromIndex, int toIndex){    modCount++;    int numMoved = size - toIndex;    System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved);    // clear to let GC do its work    int newSize = size - (toIndex - fromIndex);    for (int i = newSize; i < size; i++)    {        elementData[i] = null;    }    size = newSize;}

5)删除ArrayList中不是所传入集合类中的所有元素,针对泛型

public boolean retainAll(Collection<?> c){    Objects.requireNonNull(c);    return batchRemove(c, true);}

2.5、修改操作

1)修改指定位置上的元素

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

2.6、查找操作

1)获取指定位置上的元素

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

2)获取指定元素在列表中的第一个位置索引

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

3)获取指定元素在列表中的最后一个位置索引

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

4)返回某个范围的视图

public List<E> subList(int fromIndex, int toIndex){    subListRangeCheck(fromIndex, toIndex, size);    return new SubList(this, 0, fromIndex, toIndex);}

扩展:java List.subList方法中的超级大陷阱

 

5)迭代器

a>

public Iterator<E> iterator(){    return new Itr();}

b>

public ListIterator<E> listIterator(){    return new ListItr(0);}

扩展:JAVA中ListIterator和Iterator详解与辨析

c>

public ListIterator<E> listIterator(int index){    if (index < 0 || index > size)        throw new IndexOutOfBoundsException("Index: " + index);    return new ListItr(index);}

2.7、状态操作

1)是否为空

public boolean isEmpty(){    return size == 0;}

2.8、其它常用操作

1)清空列表所有元素

public void clear(){    modCount++;    // clear to let GC do its work    for (int i = 0; i < size; i++)        elementData[i] = null;    size = 0;}

2)克隆ArrayList实例的副本,浅拷贝

public Object clone(){    try    {        ArrayList<?> v = (ArrayList<?>) super.clone();        v.elementData = Arrays.copyOf(elementData, size);        v.modCount = 0;        return v;    } catch (CloneNotSupportedException e)    {        // this shouldn‘t happen, since we are Cloneable        throw new InternalError(e);    }}

3)判断是否包含某个指定元素

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

 

4)获取元素的个数

public int size(){    return size;}

2.9、辅助操作

1)确保容量大小

public void ensureCapacity(int minCapacity){    int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)        // any size if not default element table        ? 0        // larger than default for default empty table. It‘s already        // supposed to be at default size.        : DEFAULT_CAPACITY;    if (minCapacity > minExpand)    {        ensureExplicitCapacity(minCapacity);    }}

2)裁剪容量

public void trimToSize(){    modCount++;    if (size < elementData.length)    {        elementData = (size == 0)        ? EMPTY_ELEMENTDATA        : Arrays.copyOf(elementData, size);    }}

3)转换成数组

a> 该操作没有涉及到引用,属于安全操作

public Object[] toArray(){    return Arrays.copyOf(elementData, size);}

b> 针对泛型

public <T> T[] toArray(T[] a){    if (a.length < size)        // Make a new array of a‘s runtime type, but my contents:        return (T[]) Arrays.copyOf(elementData, size, a.getClass());    System.arraycopy(elementData, 0, a, 0, size);    if (a.length > size)        a[size] = null;    return a;}

 

 

 

 

参考:

http://www.jb51.net/article/42764.htm

http://zhangshixi.iteye.com/blog/674856

时间: 2024-08-02 06:59:02

Java中ArrayList源码分析的相关文章

Java笔记---ArrayList源码分析

一.前言 一直就想看看java的源码,学习一下大牛的编程.这次下狠心花了几个晚上的时间,终于仔细分析了下 ArrayList 的源码(PS:谁说的一个晚上可以看完的?太瞎扯了).现在记录一下所得. 二.ArrayList 源码分析 2.1 如何分析? 想要分析下源码是件好事,但是如何去进行分析呢?以我的例子来说,我进行源码分析的过程如下几步: 找到类:利用 Eclipse 找到所需要分析的类(此处就是 ArrayList) 新建类:新建一个类,命名为 ArrayList,将源码拷贝到该类.因为我

Java中HashMap源码分析

一.HashMap概述 HashMap基于哈希表的Map接口的实现.此实现提供所有可选的映射操作,并允许使用null值和null键.(除了不同步和允许使用null之外,HashMap类与Hashtable大致相同)此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 值得注意的是HashMap不是线程安全的,如果想要线程安全的HashMap,可以通过Collections类的静态方法synchronizedMap获得线程安全的HashMap. Map map = Collections.sync

java.util.ArrayList源码分析

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable 可变数组大小的List实现,允许所有的元素,包括null.(该类可粗略地看作是Vector,除了它不是同步化的) size.isEmpty.get.set.iterator和listIterator操作的运行时间是常量.add操作对于添加n个

Java - ArrayList源码分析

java提高篇(二一)-----ArrayList 一.ArrayList概述 ArrayList是实现List接口的动态数组,所谓动态就是它的大小是可变的.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小. 每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小.默认初始容量为10.随着ArrayList中元素的增加,它的容量也会不断的自动增长.在每次添加新的元素时,Array

Java集合系列之ArrayList源码分析

一.ArrayList简介 ArrayList是可以动态增长和缩减的索引序列,它是基于数组实现的List类. 该类封装了一个动态再分配的Object[]数组,每一个类对象都有一个capacity属性,表示它们所封装的Object[]数组的长度,当向ArrayList中添加元素时,该属性值会自动增加.如果想ArrayList中添加大量元素,可使用ensureCapacity方法一次性增加capacity,可以减少增加重分配的次数提高性能. ArrayList的用法和Vector向类似,但是Vect

Java ArrayList源码分析(有助于理解数据结构)

arraylist源码分析 1.数组介绍 数组是数据结构中很基本的结构,很多编程语言都内置数组,类似于数据结构中的线性表 在java中当创建数组时会在内存中划分出一块连续的内存,然后当有数据进入的时候会将数据按顺序的存储在这块连续的内存中.当需要读取数组中的数据时,需要提供数组中的索引,然后数组根据索引将内 存中的数据取出来,返回给读取程序.在Java中并不是所有的数据都能存储到数组中,只有相同类型的数据才可以一起存储到数组中.    因为数组在存储数据时是按顺序存储的,存储数据的内存也是连续的

ArrayList源码分析--jdk1.8

ArrayList概述   1. ArrayList是可以动态扩容和动态删除冗余容量的索引序列,基于数组实现的集合.  2. ArrayList支持随机访问.克隆.序列化,元素有序且可以重复.  3. ArrayList初始默认长度10,使用Object[]存储各种数据类型. ArrayList数据结构   数据结构是集合的精华所在,数据结构往往也限制了集合的作用和侧重点,了解各种数据结构是我们分析源码的必经之路.  ArrayList的数据结构如下: ArrayList源码分析 /* * 用数

java.io.BufferedOutputStream 源码分析

BufferedOutputStream  是一个带缓冲区到输出流,通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统. 俩个成员变量,一个是存储数据的内部缓冲区,一个是缓冲区中的有效字节数. /** * The internal buffer where data is stored. */ protected byte buf[]; /** * The number of valid bytes in the buffer. This value

Java split方法源码分析

Java split方法源码分析 1 public String[] split(CharSequence input [, int limit]) { 2 int index = 0; // 指针 3 boolean matchLimited = limit > 0; // 是否限制匹配个数 4 ArrayList<String> matchList = new ArrayList<String>(); // 匹配结果队列 5 Matcher m = matcher(inp