构造函数源码
有参构造函数传入一个int
public ArrayList(int capacity) {
//判断传入的值是否小于0
if (capacity < 0) {
throw new IllegalArgumentException("capacity < 0: " + capacity);
}
//如果为0则返回空数组,否则new 一个capacity的数组
array = (capacity == 0 ? EmptyArray.OBJECT : new Object[capacity]);
}
无参构造函数
public ArrayList() {
//返回一个空数组
array = EmptyArray.OBJECT;
}
有参构造函数传入一个集合
public ArrayList(Collection<? extends E> collection) {
//判断传入的集合是否为空
if (collection == null) {
throw new NullPointerException("collection == null");
}
//将集合转换为数组
Object[] a = collection.toArray();
if (a.getClass() != Object[].class) {
//new一个数组
Object[] newArray = new Object[a.length];
//将a的数组从0(第一个参数)到要赋值数组的数量(第四个参数)拷贝到newArray数组从0(第三个参数)开始
System.arraycopy(a, 0, newArray, 0, a.length);
//将newArray数组的值赋值给a
a = newArray;
}
//再将array的数组赋值为a
array = a;
//获得新数组的大小
size = a.length;
}
add源码分析
指定索引的位置添加对象
@Override public void add(int index, E object) {
//获得构造函数中赋的值
Object[] a = array;
int s = size;
//要添加的索引大于数组的大小或者小于0抛出异常
if (index > s || index < 0) {
throwIndexOutOfBoundsException(index, s);
}
//当前的大小小于数组的大小,说明可以直接插入
if (s < a.length) {
//从a的index开始,要复制的数量开始赋值给a的index+1开始
//即可直接插入的情况 把 index位置及其后面的元素(s - index个数)向后移动一个位置
System.arraycopy(a, index, a, index + 1, s - index);
} else {
//就是说数组大小不够,这时候需要进行扩容
Object[] newArray = new Object[newCapacity(s)];
System.arraycopy(a, 0, newArray, 0, index);
//此时是和之前直接插入都是先将数据全部向后移动
System.arraycopy(a, index, newArray, index + 1, s - index);
array = a = newArray;
}
//然后在将对象插入进去
a[index] = object;
//数组的大小+1
size = s + 1;
//modCount代表修改的次数
modCount++;
}
newCapacity(s)源码分析
private static int newCapacity(int currentCapacity) {
//如果当前的数组的大小为6,则大于6,则返回12进行扩容,如果小于等于6,向右移动2位进行缩容
//5 0101
//8 1010 向右移1位后的数据大小 0101=5
int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ?
MIN_CAPACITY_INCREMENT : currentCapacity >> 1);
return currentCapacity + increment;
}
添加到最后一个位置
@Override public boolean add(E object) {
Object[] a = array;
int s = size;
if (s == a.length) {
//如果当前的数组的大小小于6,则返回12进行扩容,如果大于等于6,向右移动2位进行缩容
Object[] newArray = new Object[s +
(s < (MIN_CAPACITY_INCREMENT / 2) ?
MIN_CAPACITY_INCREMENT : s >> 1)];
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray;
}
//默认添加到最后一个位置
a[s] = object;
//数组的大小加1
size = s + 1;
modCount++;
return true;
}
添加一个集合:默认添加到尾部
@Override public boolean addAll(Collection<? extends E> collection) {
Object[] newPart = collection.toArray();//首先将集合转换成数组
int newPartSize = newPart.length;//获得添加的数组的大小
if (newPartSize == 0) {
return false;
}
//旧数组的大小和数组
Object[] a = array;
int s = size;
int newSize = s + newPartSize; //如果添加溢出,arraycopy将失败。
if (newSize > a.length) {//要添加的数组的大小大于旧数组的大小的时候进行扩容
int newCapacity = newCapacity(newSize - 1); // ~33% 增长
Object[] newArray = new Object[newCapacity];
//获得最终的数组的大小
System.arraycopy(a, 0, newArray, 0, s);
array = a = newArray;
}
//从a的数组的s开始 添加
System.arraycopy(newPart, 0, a, s, newPartSize);
size = newSize;
modCount++;
return true;
}
其他源码分析
contains包含源码分析
@Override public boolean contains(Object object) {
Object[] a = array;
int s = size;
if (object != null) {//判断传入的对象是否为空
for (int i = 0; i < s; i++) {//对就数组进行遍历,查找是否有object对象
if (object.equals(a[i])) {
return true;
}
}
} else {
for (int i = 0; i < s; i++) {
if (a[i] == null) {//如果object返回为null且数组中含有空则也返回true
return true;
}
}
}
return false;
}
indexOf下标获得源码分析
@Override public int indexOf(Object object) {
Object[] a = array;
int s = size;
if (object != null) {
for (int i = 0; i < s; i++) {//对就数组进行遍历,查找是否有object对象
if (object.equals(a[i])) {
return i;有则直接返回i
}
}
} else {
for (int i = 0; i < s; i++) {
if (a[i] == null) {
return i;
}
}
}
return -1;//没有返回-1
}
查找字符或者子串是后一次出现的地方lastIndexOf源码分析,这里不贴源码了,实际就是for循环从数组的大小开始递减
移除源码分析
@Override public E remove(int index) {
Object[] a = array;
int s = size;
if (index >= s) {//数组越界
throwIndexOutOfBoundsException(index, s);
}
@SuppressWarnings("unchecked") E result = (E) a[index];
//将要移除的数组后面的数据全部往前移一位
System.arraycopy(a, index + 1, a, index, --s - index);
//之后末尾的位置赋值为空
a[s] = null;//防止内存泄漏
//大小等于s
size = s;
modCount++;//修改的次数+1
return result;
}
toArray源码分析
@Override public Object[] toArray() {
int s = size;//首先获得大小
Object[] result = new Object[s];//new一个数组
System.arraycopy(array, 0, result, 0, s);
return result;
}
ArrayListIterator迭代器源码分析
private class ArrayListIterator implements Iterator<E> {
//剩余的大小,默认是数组的大小
private int remaining = size;
//要移除的索引的位置
private int removalIndex = -1;
private int expectedModCount = modCount;
public boolean hasNext() {
//判断是否有下一个
return remaining != 0;
}
@SuppressWarnings("unchecked") public E next() {
ArrayList<E> ourList = ArrayList.this;
int rem = remaining;
if (ourList.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (rem == 0) {//剩余的为0
throw new NoSuchElementException();
}
remaining = rem - 1;//剩余的数量-1
//removalIndex = ourList.size - rem 默认等于0,rem会不断的减去1
return (E) ourList.array[removalIndex = ourList.size - rem];
}
public void remove() {
Object[] a = array;
int removalIdx = removalIndex;
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (removalIdx < 0) {
throw new IllegalStateException();
}
System.arraycopy(a, removalIdx + 1, a, removalIdx, remaining);
a[--size] = null; // Prevent memory leak
removalIndex = -1;
expectedModCount = ++modCount;
}
}