ArrayList是集合的一种实现,实现了接口List,List接口继承了Collection接口。Collection是全部集合类的父类。ArrayList使用很是普遍,不管是数据库表查询,excel导入解析,仍是网站数据爬取都须要使用到,了解ArrayList原理及使用方法显得很是重要。java
//默认建立一个ArrayList集合 List<String> list = new ArrayList<>(); //建立一个初始化长度为100的ArrayList集合 List<String> initlist = new ArrayList<>(100); //将其余类型的集合转为ArrayList List<String> setList = new ArrayList<>(new HashSet());
咱们读一下源码,看看定义ArrayList的过程到底作了什么?数据库
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { /** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10; /** * Shared empty array instance used for empty instances. */ private static final Object[] EMPTY_ELEMENTDATA = {}; /** * Shared empty array instance used for default sized empty instances. We * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when * first element is added. */ private static final Object[] DEFAULTCAPACITY_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 == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. */ transient Object[] elementData; // non-private to simplify nested class access /** * The size of the ArrayList (the number of elements it contains). * * @serial */ private int size; /** * 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) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } } /** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_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(); 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; } } }
其实源码里面已经很清晰了,ArrayList非线程安全,底层是一个Object[],添加到ArrayList中的数据保存在了elementData属性中。数组
当调用new ArrayList<>()
时,将一个空数组{}赋值给了elementData,这个时候集合的长度size为默认长度0;安全
当调用new ArrayList<>(100)
时,根据传入的长度,new一个Object[100]赋值给elementData,固然若是玩儿的话,传了一个0,那么将一个空数组{}赋值给了elementData;dom
当调用new ArrayList<>(new HashSet())时,根据源码,咱们可知,能够传递任何实现了Collection接口的类,将传递的集合调用toArray()方法转为数组内赋值给elementData;ide
注意:在传入集合的ArrayList的构造方法中,有这样一个判断网站
if (elementData.getClass() != Object[].class),ui
给出的注释是:c.toArray might (incorrectly) not return Object[] (see 6260652),即调用toArray方法返回的不必定是Object[]类型,查看ArrayList源码this
public Object[] toArray() { return Arrays.copyOf(elementData, size);}咱们发现返回的确实是Object[],那么为何还会有这样的判断呢?线程
若是有一个类CustomList继承了ArrayList,而后重写了toArray()方法呢。。
public class CustomList<E> extends ArrayList { @Override public Integer [] toArray() { return new Integer[]{1,2}; }; public static void main(String[] args) { Object[] elementData = new CustomList<Integer>().toArray(); System.out.println(elementData.getClass()); System.out.println(Object[].class); System.out.println(elementData.getClass() == Object[].class); } }
执行结果:
class [Ljava.lang.Integer; class [Ljava.lang.Object; false
接着说,若是传入的集合类型和咱们定义用来保存添加到集合中值的Object[]类型不一致时,ArrayList作了什么处理?读源码看到,调用了Arrays.copyOf(elementData, size, Object[].class);
,继续往下走
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
咱们发现定义了一个新的数组,将原数组的数据拷贝到了新的数组中去。
ArrayList有不少经常使用方法,add,addAll,set,get,remove,size,isEmpty等
首先定义了一个ArrayList,
List<String> list = new ArrayList<>(10); list.add('牛魔王'); list.add('蛟魔王'); ... list.add('美猴王');
Object[] elementData中数据以下:
咱们经过源码来看一下add("白骨精")到底发生了什么
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
首先经过 ensureCapacityInternal(size + 1)
来保证底层Object[]数组有足够的空间存放添加的数据,而后将添加的数据存放到数组对应的位置上,咱们看一下是怎么保证数组有足够的空间?
private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); }
private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }
这里首先肯定了Object[]足够存放添加数据的最小容量,而后经过 grow(int minCapacity)
来进行数组扩容
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); }
扩容规则为“数组当前足够的最小容量 + (数组当前足够的最小容量 / 2)”,即数组当前足够的最小容量 * 1.5,固然有最大值的限制。
由于最开始定义了集合容量为10,故而本次不会进行扩容,直接将第8个位置(从0开始,下标为7)设置为“白骨精”,这时Object[] elementData中数据以下:
还有和add()相似的方法。空间扩容原理都是同样,如:
add("铁扇", 0);
//将数组中的元素各自日后移动一位,再将“铁扇”放到第一个位置上;
addAll(list..七个葫芦娃);
//将集合{七个葫芦娃}放到"白骨精"后,很明显当前数组的容量已经不够,须要扩容了,不执行该句代码了;
addAll(list..哪吒三兄弟, 4);
//从第五个位置将“哪吒三兄弟”插进去,那么数组第五个位置后的元素都需日后移动三位,数组按规则扩容为18。
指定了插入位置的,会经过rangeCheckForAdd(int index)方法判断是否数组越界
由于ArrayList底层是由数组实现的,set实现很是简单,调用 set(8, "猪八戒")
经过传入的数字下标找到对应的位置,替换其中的元素,前提也须要首先判断传入的数组下标是否越界。将“猕猴王”替换为“猪八戒”。
public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; }
//返回值“猕猴王”,当前数组中数据:
ArrayList中get方法也很是简单,经过下标查找便可,同时须要进行了类型转换,由于数组为Object[],前提是须要判断传入的数组下标是否越界。
public E get(int index) { rangeCheck(index); return elementData(index); } E elementData(int index) { return (E) elementData[index]; }
调用get(6)返回”哪吒“。
首先说一下ArrayList经过下标删除的方法,咱们看一下源码
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; }
经过源码咱们能够看到首先获取了待删除的元素,并最终返回了。其次计算了数组中须要移动的位数 size - index - 1,那么很明显咱们能够得出待删除的是最后一个元素的话,移到位数为0,不然移动位数大于0,那么经过数组元素的拷贝来实现往前移动相应位数。
如remove(10),找到的元素为“美猴王”,那么移动位数 = 12-10-1 = 1;此时将本来在第12个位置上(数组下标为11)的“白骨精”往前移动一位,同时设置elementData[11] = null;这里经过设置null值让GC起做用。
删除ArrayList中的值对象,其实和经过下标删除很类似,只是多了一个步骤,遍历底层数组elementData,经过equals()方法或 == (特殊状况下)来找到要删除的元素,获取其下标,调用remove(int index)同样的代码便可。
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; }
size() : 获取集合长度,经过定义在ArrayList中的私有变量size获得
isEmpty():是否为空,经过定义在ArrayList中的私有变量size获得
contains(Object o):是否包含某个元素,经过遍历底层数组elementData,经过equals或==进行判断
clear():集合清空,经过遍历底层数组elementData,设置为null
本文主要讲解了ArrayList原理,从底层数组着手,讲解了ArrayList定义时到底发生了什么,再添加元素时,扩容规则如何,删除元素时,数组的元素的移动方式以及一些经常使用方法的用途,如有不对之处,请批评指正,望共同进步,谢谢!