Skip to content

Instantly share code, notes, and snippets.

@lichenbo
Created July 11, 2013 14:16
Show Gist options
  • Save lichenbo/5975832 to your computer and use it in GitHub Desktop.
Save lichenbo/5975832 to your computer and use it in GitHub Desktop.
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
protected AbstractList() => 空方法
public boolean add(E e) => 调用含有index的add
abstract public E get(int index) => 未实现
public E set(int index, E element) => 抛出异常
public void add(int index, E element) => 抛出异常
public E remove(int index) => 抛出异常
public int indexOf(Object o) => 调用ListIterator进行查找
public int lastIndexOf(Object o) => 调用ListIterator进行查找
public void clear() => 调用RemoveRange
public boolean addAll(int index, Collection<? extends E> c) => 循环调用add
public Iterator<E> iterator() => 返回Itr()实例
public ListIterator<E> listIterator() => 返回ListIterator实例
public ListIterator<E> listIterator(final int index) => 返回ListIterator实例(边界检查)
private class Itr implements Iterator<E> => 一系列Iterator操作,next()用AbstractList.get实现,remove()用AbstractList.remove实现
private class ListItr extends Itr implements ListIterator<E> => 加强版Iterator。均调用AbstractList中的方法实现。
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
ListIterator<E> e1 = listIterator();
ListIterator e2 = ((List) o).listIterator();
while (e1.hasNext() && e2.hasNext()) {
E o1 = e1.next();
Object o2 = e2.next();
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
public int hashCode() {
int hashCode = 1;
for (E e : this)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
protected void removeRange(int fromIndex, int toIndex) => 调用Iterator.remove()方法
protected transient int modCount = 0;
private void rangeCheckForAdd(int index) => RangeCheck
class SubList<E> extends AbstractList<E> => 一切关于sublist的东西
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
super(list, fromIndex, toIndex);
}
public List<E> subList(int fromIndex, int toIndex) {
return new RandomAccessSubList<>(this, fromIndex, toIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment