arrayList源码分析
private static final int DEFAULT_CAPACITY = 10;
初始容量为10;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
最大容量
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);
}
}
public ArrayList() {//空参,默认为10
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
添加方法(不加索引)
public boolean add(E e) {//添加方法
ensureCapacityInternal(size + 1); // 这里有点不明白size没有赋值应该不能直接使用的。
elementData[size++] = e;//将数据插在size处,插入完成之后size+1
return true;
}
---------------------------------------
private void ensureCapacityInternal(int minCapacity) {
//add中的方法,如果数组初始为空,进入if
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
--------------------------------------------------
private void ensureExplicitCapacity(int minCapacity) {
modCount++;//这个成员变量记录着集合的修改次数,也就每次add或者remove它的值都会加1
// 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);//如果容量小于4,每次添加元素都会扩容一次,容量加一
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);
}
添加方法(加索引)
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);//先将index及之后的数据向后移
elementData[index] = element;//将数据插入index处
size++;
}
------------------------------------------
private void rangeCheckForAdd(int index) {//判断索引是否越界
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
通过索引删除
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);//记录index处的值,用来返回
int numMoved = size - index - 1;//如果删除的是最后一个,就不用进行copy
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);//将index后面的数据前移一位
elementData[--size] = null; // clear to let GC do its work
return oldValue;
--------------------------------------------------------
private void rangeCheck(int index) {
if (index >= size)//**注意这里的限制**
throw new IndexOutOfBoundsException(outOfBoundsMsg(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;
}
注意如果是Integer类型,通过数据数据删除。
List<Integer> list = new ArrayList(1);
list.add(1);
list.add(5);
list.remove(5);//运行时异常
list.remove(new Integer(5));//正确
addAll方法
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
get方法![get方法只有index](https://i-blog.csdnimg.cn/blog_migrate/9ef35cfb19a54795113bd95e0ecf3226.png#pic_center)
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
--------------------------------------------------
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
//报错,并返回index,和size值
}
------------------------------------------------
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;//
}
set方法
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
-------------------------------------------