简介:

  ArrayList是我们开发中非常常用的数据存储容器之一,其底层是数组实现的,我们可以在集合中存储任意类型的数据,ArrayList是线程不安全的,非常适合用于对元素进行查找,效率非常高。

线程安全性:

  对ArrayList的操作一般分为两个步骤,改变位置(size)和操作元素(e)。所以这个过程在多线程的环境下是不能保证具有原子性的,因此ArrayList在多线程的环境下是线程不安全的。

源码分析:

1.属性分析:

    /**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10; /**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {}; /**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == EMPTY_ELEMENTDATA will be expanded to
* DEFAULT_CAPACITY when the first element is added.
*/
private transient Object[] elementData; /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;

扩展:什么是序列化
序列化是指:将对象转换成以字节序列的形式来表示,以便用于持久化和传输。
实现方法:实现Serializable接口。
然后用的时候拿出来进行反序列化即可又变成Java对象。

transient关键字解析
Java中transient关键字的作用,简单地说,就是让某些被修饰的成员属性变量不被序列化。
有了transient关键字声明,则这个变量不会参与序列化操作,即使所在类实现了Serializable接口,反序列化后该变量为空值。

那么问题来了:ArrayList中数组声明:transient Object[] elementData;,事实上我们使用ArrayList在网络传输用的很正常,并没有出现空值。
原来:ArrayList在序列化的时候会调用writeObject()方法,将size和element写入ObjectOutputStream;反序列化时调用readObject(),从ObjectInputStream获取size和element,再恢复到elementData。

那为什么不直接用elementData来序列化,而采用上述的方式来实现序列化呢?
原因在于elementData是一个缓存数组,它通常会预留一些容量,等容量不足时再扩充容量,那么有些空间可能就没有实际存储元素,采用上诉的方式来实现序列化时,就可以保证只序列化实际存储的那些元素,而不是整个数组,从而节省空间和时间。

2.构造方法分析

根据initialCapacity 初始化一个空数组,如果值为0,则初始化一个空数组:

    /**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}

不带参数初始化,默认容量为10:

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

通过集合做参数的形式初始化:如果集合为空,则初始化为空数组:

    /**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
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);
}

3.主干方法

trimToSize() 用来最小化实例存储,将容器大小调整为当前元素所占用的容量大小。

    /**
* Trims the capacity of this <tt>ArrayList</tt> instance to be the
* list's current size. An application can use this operation to minimize
* the storage of an <tt>ArrayList</tt> instance.
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}
clone()方法:用来克隆出一个新数组。
    /**
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
* elements themselves are not copied.)
*
* @return a clone of this <tt>ArrayList</tt> instance
*/
public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) 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();
}
}

通过调用Object的clone()方法来得到一个新的ArrayList对象,然后将elementData复制给该对象并返回。

add(E e)方法:在数组末尾添加元素

    /**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}

看到它首先调用了ensureCapacityInternal()方法.注意参数是size+1,这是个面试考点

    private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
} ensureExplicitCapacity(minCapacity);
}

这个方法里又嵌套调用了两个方法:计算容量+确保容量
计算容量:如果elementData是空,则返回默认容量10和size+1的最大值,否则返回size+1

计算完容量后,进行确保容量可用:(modCount不用理它,它用来计算修改次数)
如果size+1 > elementData.length证明数组已经放满,则增加容量,调用grow()。

    private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

增加容量:默认1.5倍扩容。
获取当前数组长度=>oldCapacity
oldCapacity>>1 表示将oldCapacity右移一位(位运算),相当于除2。再加上1,相当于新容量扩容1.5倍。
如果newCapacity&gt;1=1,1&lt;2所以如果不处理该情况,扩容将不能正确完成。
如果新容量比最大值还要大,则将新容量赋值为VM要求最大值。
将elementData拷贝到一个新的容量中。

    /**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
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);
}

size+1的问题

好了,那到这里可以说一下为什么要size+1。
size+1代表的含义是:
如果集合添加元素成功后,集合中的实际元素个数。
为了确保扩容不会出现错误。
假如不加一处理,如果默认size是0,则0+0>>1还是0。
如果size是1,则1+1>>1还是1。有人问:不是默认容量大小是10吗?事实上,jdk1.8版本以后,ArrayList的扩容放在add()方法中。之前放在构造方法中。我用的是1.8版本,所以默认ArrayList arrayList = new ArrayList();后,size应该是0.所以,size+1对扩容来讲很必要.

add(int index, E element)方法

    /**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

rangeCheckForAdd()是越界异常检测方法。

    /**
* A version of rangeCheck used by add and addAll.
*/
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

ensureCapacityInternal()之前有讲,着重说一下System.arrayCopy方法:

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

代码解释:
Object src : 原数组
int srcPos : 从元数据的起始位置开始
Object dest : 目标数组
int destPos : 目标数组的开始起始位置
int length : 要copy的数组的长度
示例:size为6,我们调用add(2,element)方法,则会从index=2+1=3的位置开始,将数组元素替换为从index起始位置为index=2,长度为6-2=4的数据。

set(int index,E element)方法:逻辑很简单,覆盖旧值并返回。

        public E set(int index, E e) {
rangeCheck(index);
checkForComodification();
E oldValue = ArrayList.this.elementData(offset + index);
ArrayList.this.elementData[offset + index] = e;
return oldValue;
}

indexOf(Object o)方法:根据Object对象获取数组中的索引值。

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

如果o为空,则返回数组中第一个为空的索引;不为空也类似。
注意:通过源码可以看到,该方法是允许传空值进来的。

get(int index)方法:返回指定下标处的元素的值。

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

rangeCheck(index)会检测index值是否合法,如果合法则返回索引对应的值。

remove(int index)方法:删除指定下标的元素。

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

这里又碰到了System.arraycopy()方法,详情请查阅上文。
大概思路:将该元素后面的元素前移,最后一个元素置空。

ArrayList优缺点
优点:
因为其底层是数组,所以修改和查询效率高。
可自动扩容(1.5倍)。
缺点:
插入和删除效率不高。
线程不安全。

手写简易ArrayList:

package basic;

public class MyArrayList {

    // 非私有,以简化嵌套类访问
// transient 在已经实现序列化的类中,不允许某变量序列化
transient Object[] elementData; // 默认容量
private static final int DEFAULT_CAPACITY = 10; // 用于空实例的 空数组实例
private static final Object[] EMPTY_ELEMENTDATA = {}; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
// 实际ArrayList集合大小
private int size; /**
* 构造方法
*/
public MyArrayList(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);
}
} public MyArrayList() {
this(DEFAULT_CAPACITY);
} public void add(Object o) {
// 1. 判断数据容量是否大于 elementData
ensureExplicitCapacity(size + 1);
// 2. 使用下标进行赋值
elementData[size++] = o;
} private void ensureExplicitCapacity(int minCapacity) {
if (size == elementData.length) {
// 需要扩容,扩容1.5倍(ArrayList默认扩容1.5倍)
// 注意:如果oldCapacity值为1
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
// 如果新容量 < 最小容量, 则将最小容量赋值给新容量
// 如果 oldCapacity=1, 则 minCapacity=1+1=2 newCapacity=1+(1>>1)=1
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
// 创建新数组
Object[] objects = new Object[newCapacity];
// 将数据复制给新数组
System.arraycopy(elementData, 0, objects, 0, elementData.length);
// 修改引用
elementData = objects;
}
} public Object get(int index) {
rangeCheck(index);
return elementData[index];
} private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException("下标越界");
} /**
* 通过下标删除
* @param index
* @return
*/
public Object remove(int index) {
rangeCheck(index); //modCount++;
// 先查出元素
Object oldValue = elementData[index];
// 找出置换结束位置
int numMoved = size - index - 1;
if (numMoved > 0)
// 从 index+1 开始 将值覆盖为 index-numMoved 的值
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) {
for (int index = 0; index < size; index++) {
if (o.equals(elementData[index])) {
remove(index);
return true;
}
}
return false;
}
}

modCount的作用

ArrayList源码解读笔记的更多相关文章

  1. ArrayList 源码解读

    ArrayList 源码解读     基于JDk 1.7.0_80 public class ArrayList<E> extends AbstractList<E> impl ...

  2. ArrayList源码分析笔记

    ArrayList源码分析笔记 先贴出ArrayList一些属性 public class ArrayList<E> extends AbstractList<E> imple ...

  3. js便签笔记(10) - 分享:json2.js源码解读笔记

    1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...

  4. js便签笔记(10) - 分享:json.js源码解读笔记

    1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...

  5. JDK12下的ArrayList源码解读 与 Vector的对比

    ArrayList源码阅读. //测试代码实现如下 private static void arrayList() { ArrayList<String> list = new Array ...

  6. ArrayList源码解读(jdk1.8)

    概要 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具体实现类进行讲解:首先,讲解List,而List中ArrayList又最为常用.因此,本章我们讲解Arra ...

  7. 深入理解JAVA集合系列四:ArrayList源码解读

    在开始本章内容之前,这里先简单介绍下List的相关内容. List的简单介绍 有序的collection,用户可以对列表中每个元素的插入位置进行精确的控制.用户可以根据元素的整数索引(在列表中的位置) ...

  8. Java集合ArrayList源码解读

    最近在回顾数据结构,想到JDK这样好的代码资源不利用有点可惜,这是第一篇,花了心思.篇幅有点长,希望想看的朋友认真看下去,提出宝贵的意见.  :) 内部原理 ArrayList 的3个字段 priva ...

  9. java集合之ArrayList源码解读

    源自:jdk1.8.0_121 ArrayList继承自AbstractList,实现了List.RandomAccess.Cloneable.Serializable. ArrayList内部是通过 ...

随机推荐

  1. DB2(Procedure)存储过程遍历循环!

    有时候一些复杂的业务逻辑将要通过存储过程的循环语句进行处理;以下列出2种DB2存储过程的循环语句,方便以后的查看并使用! 推荐第一种方式的使用,最大的优点就是比较直观;在需要操作很多字段的情况下,不需 ...

  2. day34 并发编程之生产者消费者模型 队列

    1.守护进程(了解) """ 守护进程 表示 一个进程b 守护另一个进程a 当被守护的进程a结束后 那么b也跟着结束了 就像 皇帝驾崩 妃子殉葬 应用场景 之所以开启子进 ...

  3. object references an unsaved transient instance save the transient instance before flushing

    object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...

  4. git-如何不写注释能自动带上修改文件信息

    背景:每次提交git,都要写注释,有些情况注释不太好写,或者根本没有必要写,这时可以通过自动加注释方法,比如可以追加修改了哪些文件 解决:通过shell脚本,在脚本里面写git命令,add commi ...

  5. Linux 只列出目录的方法

    1. ls -d 2. find -type d -maxdepth 1 3. ls -F | grep "/$" 4. ls -l | grep "^d"

  6. Redhat/Centos6.x安装Chrome

    由于Chrome对rhel6.x不在支持发布版本,只能安装chromium版本! 01.下载地址 http://people.centos.org/hughesjr/chromium/6/x86_64 ...

  7. linux虚拟机配置上网(静态IP)和配置tomcat服务环境

    常用命令:vi或者vim编辑 ,按i编辑模式,按ecs进入基本模式,按 :w  保存:按 :wq  退出并保存:mv移动::q退出 :ln -sv apache-tomcat-8.0.24 tomca ...

  8. PHPNow升级PHP版本

    PHPNow升级PHP版本 phpnow下载地址:http://www.jb51.net/softs/12868.html 1,先把PHP5.3.5下载下来,在官网我是没找到VC6的版本,只能从Goo ...

  9. maven +bootstrap+ssm

    http://blog.csdn.net/yangwenxue_admin/article/details/71757505

  10. python基本数据类型之字符串(五)

    python基本数据类型之字符串(五) 遍历与查找 python中的字符串属于可迭代对象,通过一些方法可以遍历字符串中的每一个字符.而查找的方法主要有两个:find与index. 1.字符串的遍历 字 ...