ArrayList源码分析
提前须知
1.arrylist最大容量 : MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8 = 2147483639 = 2^31-1
2.默认容量 : DEFAULT_CAPACITY = 10
3.底层为数组 : 所有的操作都是基于数组elementdata[] : transient Object[] elementData;
4.transient修饰的变量不能被序列化
5.new ArrayList()时 : 括号内数字大于0 ,则取括号内长度 ,等于0 ,则为10 小于0 报new throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
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);
}
}
6.public ArrayList(Collection<? extends E> c) {
elementData = c.toArray(); //将c转化为elementdata
if ((size = elementData.length) != 0) { //判断长度不为0 , 同时赋值给size
if (elementData.getClass() != Object[].class) // 数组类型不是Object[]
elementData = Arrays.copyOf(elementData, size, Object[].class);//进行转化
} else { //长度为0,创建新数组
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
add()方法
public boolean add(E e) {
ensureCapacityInternal(size + 1); // 扩容操作(具体就是 : 先在原有size+1判断是否达到上限,是否扩容)
elementData[size++] = e; //由此可见list的有序性
return true;
}
由此方法可以看出ArrayList的底层为数组,在添加元素同时进行size++
ensureCapacityInternal()方法 , 即具体的扩容流程
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
//calculateCapacity(elementData, minCapacity)操作
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { //当前elementdata是否为{}
return Math.max(DEFAULT_CAPACITY, minCapacity); //返回10 和size+1中最大的值
}
return minCapacity;//否则 返回size+1;
}
// ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));操作
private void ensureExplicitCapacity(int minCapacity) {
modCount++; //计数标识
// overflow-conscious code
if (minCapacity - elementData.length > 0) //(size+1)是否大于elementdata长度
grow(minCapacity); //扩容具体实现
}
grow()方法 ,扩容具体实现
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); //新数组长度 此结果为原长度的1.5倍
//右移一位为原来的一半
if (newCapacity - minCapacity < 0) //新数组长度小于传入的长度
newCapacity = minCapacity; //赋值给newCapacity
if (newCapacity - MAX_ARRAY_SIZE > 0) //newCapacity >2147483639
newCapacity = hugeCapacity(minCapacity); //执行此方法获取最大长度
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);//数组赋值
}
private static int hugeCapacity(int minCapacity) { //此方法表示arraylist最大长度为2147483639+8
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
get()方法
public E get(int index) {
(2) rangeCheck(index); //判断数组是否越位
(1) return elementData(index); //直接返回该角标的值
}
(2)private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
(1)E elementData(int index) {
return (E) elementData[index];
}