最近看了数据结构与算法分析(Java语言描述)书中表一章节,总结下。
List继承了Collection接口,因此包含Collection接口所有方法
先看ArrayList的add()方法
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
1、ensureCapacityInternal()确保容量够用
2、ArrayList实现是elementData[]数组,将元素e插入数组
查看下插入时扩容方法,跟进ensureCapacityInternal()
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
1、若是空List,则返回容量为Math.max(DEFAULT_CAPACITY, minCapacity),默认为10,否则返回minCapacity
2、主要看grow()扩容算法
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
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);
}
1、得到数组的旧容量,然后进行oldCapacity + (oldCapacity >> 1),将oldCapacity 右移一位,其效果相当于oldCapacity /2,我们知道位运算的速度远远快于整除运算,整句运算式的结果就是将新容量更新为旧容量的1.5倍
2、然后检查新容量是否大于最小需要容量,若还是小于最小需要容量,那么就把最小需要容量当作数组的新容量
3、再检查新容量是否超出了ArrayList所定义的最大容量,若超出了,则调用hugeCapacity()来比较minCapacity和 MAX_ARRAY_SIZE,如果minCapacity大于最大容量,则新容量则为ArrayList定义的最大容量,否则,新容量大小则为 minCapacity。 (在判断容量是否超过MAX_ARRAY_SIZE的值,MAX_ARRAY_SIZE值为Integer.MAX_VALUE - 8,比int的最大值小8,不知道为什设计,可能方便判断吧。如果已经超过,调用hugeCapacity方法检查容量的int值是不是已经溢出。一般很少用到int最大值的情况,那么多数据也不会用ArrayList来做容器了,估计这辈子没机会见到hugeCapacity运行一次了。)
4、使用Arrays.copyOf方法来生成新的数组
/**
* Copies the specified array, truncating or padding with nulls (if necessary)
* so the copy has the specified length. For all indices that are
* valid in both the original array and the copy, the two arrays will
* contain identical values. For any indices that are valid in the
* copy but not the original, the copy will contain <tt>null</tt>.
* Such indices will exist if and only if the specified length
* is greater than that of the original array.
* The resulting array is of the class <tt>newType</tt>.
*
* @param <U> the class of the objects in the original array
* @param <T> the class of the objects in the returned array
* @param original the array to be copied
* @param newLength the length of the copy to be returned
* @param newType the class of the copy to be returned
* @return a copy of the original array, truncated or padded with nulls
* to obtain the specified length
* @throws NegativeArraySizeException if <tt>newLength</tt> is negative
* @throws NullPointerException if <tt>original</tt> is null
* @throws ArrayStoreException if an element copied from
* <tt>original</tt> is not of a runtime type that can be stored in
* an array of class <tt>newType</tt>
* @since 1.6
*/
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
再看下LinkedList的add()方法
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
继续跟进
/**
* Links e as last element.
*/
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++;
}
查看Node<>构造方法
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
1、发现LinkedList底层实现是双链表,有指向前一节点的prev链和指向后一节点的next链以及节点element;
2、已知指向最后节点的指针last,构建新节点newNode,并赋值成last节点,若List为空,则newNode为first节点,否则节点拼接,最后size+1
总结:
1、可看到这两种List的add()方法(若忽略ArrayList偶尔扩容问题)的时间复杂度是一致的,都为O(1);
2、ArrayList数组实现,查找根据下标时间复杂度O(1),LinkedList双链表实现,查找需从第一结点开始遍历链表,O(n);
3、ArrayList插入删除操作最坏情况在位置0操作,需移动后面整个数组,最好情况在数组末尾操作O(1),综合O(n);
4、LinkedList插入删除操作需遍历链表找到操作位置,之后更新前一节点的next链与操作结点的prev链;so,链表插入速度不一定比数组快。
下面是LinkedList的get()方法
/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
5、LinkedList提供addFirst和removeFirst、addLast和removeLast、以及getFirst和getLast方法等以有效的添加、删除和访问链表的两端的项。
最后疑问:
为啥会有双链表?
1、单链表情况下,删除某一结点时,须知前驱结点,然后再更新链表,so,双链表遍历一次后(因为是双链表,有前驱和后驱链,可二分法查找,效率更高),取前驱即可;而单链表定位到待删除节点后需再次定位前驱节点,遍历两遍。
2、双链表可二分法首尾同时查找
但为何市面上单链表多于双链表?
从空间复杂度讲,双链表比单链表每个节点多一个前驱指针,占用空间效率低。
后续:
Iterator接口——迭代器
该接口定义三个方法
public interface Iterator<AnyType>{
boolean hasNext();
AnyType next();
void remove();
}
每次调用next都给出集合的下一项,hasNext告诉是否存在下一项,remove删除项。
迭代器的remove方法比用Collection的remove方法潜藏着更高的效率。
若对正在被迭代的集合结构进行改变,那么迭代器就不再合法,会产生ConcurrentModificationException异常;只有在需要使用迭代器的时候才应该获取迭代器,若迭代器调用自己的remove方法,这个迭代器仍是合法的。
关于时间效率,看以下两个方法:
public static void removeEvensVer1(List<Integer> lst) {
for (Integer x : lst) {
if (x % 2 == 0) {
lst.remove(x);
}
}
}
public static void removeEvensVer2(List<Integer> lst) {
Iterator<Integer> itr = lst.iterator();
while (itr.hasNext()) {
if (itr.next() % 2 == 0) {
itr.remove();
}
}
}
都是将List中偶数项移除,ArrayList上remove效率很低,因为要移动数组,因此removeEvensVer1方法花费的是二次时间;LinkedList的get效率不高,需要遍历,因此也花费二次时间。
而用迭代器一步步遍历则该表则是高效的,对于LinkedList,迭代器的remove方法调用只花费常数时间,整个程序花费线性时间而不是二次时间;但对于ArrayList,remove操作仍是昂贵的,整个程序仍然花费二次时间。
开发时,一般情况用ArrayList,除非遇到频繁插入、删除的时候用LinkedList。