其他 源码解析 https://blog.csdn.net/qq_32726809/article/category/8035214
类的声明
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
- extends AbstractCollection<E> 继承抽象类
- 接口已经将 Collection 大体的骨架写了出来
- 如果要实现不可变集合,只需要继承这个类,实现iterator 和 size两个方法
- 如果需要实现可变集合,需要继承这个类,需要实现add方法,并且迭代器的remove方法也要实现
- implements List 实现list接口
- 有序集合,能够根据索引进行控制
2常用方法
由于是继承list接口和实现抽象类,所以很多方法以前都介绍过,这里只说必要的方法
2.1indexOf
public int indexOf(Object o) {
ListIterator<E> it = listIterator();/*-------1*/
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return it.previousIndex();/*-------2*/
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
- 1处的意思是 列表迭代器,继承自 Iterator
- 新增从后向前遍历hasPrevious(), previous(), previousIndex(),
- 2返回当前索引
2.2iterator()
public Iterator<E> iterator() {
return new Itr();
}
Itr 内部类源码
- 这个是实现的 Iterator,实现了迭代器的 next,hasnext,remove方法
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
2.3listIterator
public ListIterator<E> listIterator(final int index) {
rangeCheckForAdd(index);
return new ListItr(index);
}
ListItr 内部类源码
- 在继承Itr的基础上实现 ListIterator接口,使得多了 从尾部遍历的方法
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public E previous() {
checkForComodification();
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;
return previous;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor-1;
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
subList
利用内部类调用
public List<E> subList(int fromIndex, int toIndex) {
return (this instanceof RandomAccess ?
new RandomAccessSubList<>(this, fromIndex, toIndex) :
new SubList<>(this, fromIndex, toIndex));
}