一、ArrayList动态数组列表
====================================================
super();
this.elementData = EMPTY_ELEMENTDATA;
}
1.add()添加元素
到elementData,从index【要删除元素的位置】开始
移动【numMoved = size - 1 - index】位
二、LinkedList动态链表
====================================================
已有元素a0,a1,a2
add(a3)的动作
2.remove()删除元素
1>删头节点
2>删尾节点
3>删中间节点
====================================================
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;//默认初始化容量大小
//The array buffer into which the elements of the ArrayList are stored
private transient Object[] elementData;//数据存储区
//The size of the ArrayList (the number of elements it contains).
private int size;//数组中元素个数
private static final Object[] EMPTY_ELEMENTDATA = {};//空数组
}
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
1.add()添加元素
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;//赋值
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {//EMPTY_ELEMENTDATA={}
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);//最小容量不小于10
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0)//初始容量为10,add第11个元素的时候扩容
grow(minCapacity);//扩容
}
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);//新容量为oldCapacity + oldCapacity/2
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.remove()删除元素
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;size减1
return oldValue;
}
private void rangeCheck(int index) {//判断index是否越界
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
System.arraycopy(elementData, index+1, elementData, index, numMoved);
移动elementData,从index+1【要删除元素的后一位】开始,到elementData,从index【要删除元素的位置】开始
移动【numMoved = size - 1 - index】位
如上图,要删除index=1 b元素,要移动c,d,e元素至1,2,3的位置.
队列Queue是每次扩容的时候,进行System.arraycopy,ArrayList是每次remove()了都进行System.arraycopy(如果需要)
二、LinkedList动态链表
====================================================
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
transient int size = 0;
//Pointer to first node
transient Node<E> first;//链头
//Pointer to last node.
transient Node<E> last;//链尾
}
private static class Node<E> {//节点
E item;
Node<E> next;//左指针
Node<E> prev;//右指针
Node(Node<E> prev, E element, Node<E> next) {
this.prev = prev;
this.item = element;
this.next = next;
}
}
1.add()添加元素
public boolean add(E e) {
linkLast(e);//添加到链的后面
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
添加到链的后面
已有元素a0,a1,a2
add(a3)的动作
2.remove()删除元素
public E remove(int index) {
//验证下标越界index>=0 && index<size
checkElementIndex(index);
return unlink(node(index));
}
Node<E> node(int index) {
//index如果小于size值,从链头部开始查找;否则从尾部查找
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;//通过循环node.next的次数(index)找到元素
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;//同理
return x;
}
}
E unlink(Node<E> x) {
final E element = x.item;
final Node<E> prev = x.prev;
final Node<E> next = x.next;
//==========================
if (prev == null) {//删除头节点
first = next;//头指针后移
} else {
prev.next = next;
x.prev = null;
}
if (next == null) {//删除尾节点
last = prev;//尾指针前移
} else {
next.prev = prev;
x.next = null;
}
//==========================
x.item = null;
size--;
modCount++;
return element;
}
三种情况:
1>删头节点
2>删尾节点
3>删中间节点