ArrayList实现
集合概述:集合存在于java.util包路径下
特点:
重复性:数据可以重复
null值:可以有 null值存在
有序性:能保证数据的插入有序
常用方法介绍
int size(); 集合中存储元素的个数
boolean isEmpty(); 判断当前集合是否为空,返回值是Boolean类型 :false:集合不为空 true:集合为空
boolean contains(Object o); 判断当前集合是否包含该Object对象
Iterator iterator(); 迭代器,返回iterator实例
Object[] toArray(); 将集合转化为数组
T[] toArray(T[] a); 将集合转化为数组
boolean add(E e); 添加元素
boolean remove(Object o); 删除元素
boolean containsAll(Collection<?> c); 判断入参集合是否属于当前集合
boolean addAll(Collection<? extends E> c); 对该集合添加子集合形成新的集合
boolean addAll(int index, Collection<? extends E> c);在指定的位置添加子集合
void clear(); 将集合清除掉
boolean equals(Object o); 判断是否相等
E get(int index); 通过指定位置来获取元素
int indexOf(Object o); 判断当前元素在集合中的位置(从集合头部往后查找)
int lastIndexOf(Object o);判断当前元素在集合中的位置(从集合尾部往前查找)
List subList(int fromIndex, int toIndex); 找当前集合的子集
源码探究
继承关系
public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
ArrayList继承AbstractList,父类中对部分接口进行实现
实现了List接口提供的方法,Serializable说明该类能够被序列化 ,能够被克隆、序列化
底层数据结构
ArrayList底层数据结构是数组
基本属性
private static final int DEFAULT_CAPACITY = 10;
默认容量大小
private static final Object[] EMPTY_ELEMENTDATA = {};
默认数组大小
private transient Object[] elementData;
存储元素的数组
private int size;
集合存储元素的个数
构造函数
public ArrayList(int initialCapacity) {
super();
//指定大小不合法,则直接抛出异常
初始化数组大小
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
//有参构造,指定集合初始化大小
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
//无参构造
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);
}
//有参构造,通过集合来创建新的集合
增长方式
按照原数组的1.5倍进行扩容
add()添加元素
首先需要进行扩容考虑,如果要扩容(size+1 > table.lengrh),按照1.5倍进行扩容
将元素插入数组尾部,并对计数size+1
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
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) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(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);
}
remove()删除元素
先找到元素存储位置(null和非null的对象判断相等的不同)
数组移动
注意:相同元素存在的情况下,只需找到从0号开始第一次出现的元素
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;
}
private void fastRemove(int index) {
modCount++;
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
}
get()获取元素
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
E elementData(int index) {
return (E) elementData[index];
}