java源码--AbstractList
package java.util; public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { /** *唯一的构造函数。(对于子类构造函数的调用,通常是隐式的。) */ protected AbstractList() { } //添加元素,调用add(size(), e) public boolean add(E e) { add(size(), e); return true; } /** * 唯一的抽象函数*/ abstract public E get(int index); /** * 获取集合中的函数 * {@code UnsupportedOperationException}.*/ public E set(int index, E element) { throw new UnsupportedOperationException(); } /** * 添加元素*/ public void add(int index, E element) { throw new UnsupportedOperationException(); } /** * 删除元素*/ public E remove(int index) { throw new UnsupportedOperationException(); } // Search Operations 搜索操作 /** * 列表循环,找出元素第一次出现的位置*/ public int indexOf(Object o) { ListIterator<E> it = listIterator(); if (o==null) { while (it.hasNext()) if (it.next()==null) return it.previousIndex(); } else { while (it.hasNext()) if (o.equals(it.next())) return it.previousIndex(); } return -1; } /** * 列表循环,找出元素最后一次出现的位置*/ public int lastIndexOf(Object o) { ListIterator<E> it = listIterator(size()); if (o==null) { while (it.hasPrevious()) if (it.previous()==null) return it.nextIndex(); } else { while (it.hasPrevious()) if (o.equals(it.previous())) return it.nextIndex(); } return -1; } // Bulk Operations 批量操作 /** * Removes all of the elements from this list (optional operation).*/ public void clear() { removeRange(0, size()); } /** * 集合中添加集合*/ public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index); //效验index在范围之内 0< index < size() boolean modified = false; for (E e : c) { //循环添加 add(index++, e); modified = true; } return modified; } // Iterators 迭代器 /** * 按适当的顺序对列表中的元素返回一个迭代器。 * @return an iterator over the elements in this list in proper sequence */ public Iterator<E> iterator() { return new Itr(); }
public ListIterator<E> listIterator() { return listIterator(0); } /** * 此实现返回一个简单的实现,类的实现的扩展*/ public ListIterator<E> listIterator(final int index) { rangeCheckForAdd(index); //检查下标是否越界 return new ListItr(index); } private class Itr implements Iterator<E> { /** * 对next的后续调用将返回的元素的索引。 */ int cursor = 0; /** * 最近一次调用next时,在返回的元素的索引 * 之前的。如果此元素被调用删除,则重置为-1 */ int lastRet = -1; /** * 迭代器认为支持列表应该具有的modCount值。如果违背了这个期望,迭代器就会检测到并发修改 */ 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(); } }
// 删除集合内容 public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); //删除当前索引元素 if (lastRet < cursor) cursor--; //游标后退一个索引值 lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } // 防止两个线程同时对列表进行操作,一个读,一个修改,这样可能会出现我要读的元素刚好被删了,此时会发生异常。并发异常 final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } 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(); } } } //判断是否实现了RandomAccess接口,此接口的作用:https://www.cnblogs.com/yeya/p/9950723.html public List<E> subList(int fromIndex, int toIndex) { return (this instanceof RandomAccess ? new RandomAccessSubList<>(this, fromIndex, toIndex) : new SubList<>(this, fromIndex, toIndex)); } // Comparison and hashing 比较和散列 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) { ListIterator<E> it = listIterator(fromIndex); for (int i=0, n=toIndex-fromIndex; i<n; i++) { it.next(); it.remove(); } } protected transient int modCount = 0; //检查范围 private void rangeCheckForAdd(int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size(); } } class SubList<E> extends AbstractList<E> { private final AbstractList<E> l; //一个AbstractList类的引用,代表的是要被截取的List private final int offset; //下标移动量 private int size; //元素个数 SubList(AbstractList<E> list, int fromIndex, int toIndex) {
//数据效验 if (fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); if (toIndex > list.size()) throw new IndexOutOfBoundsException("toIndex = " + toIndex); if (fromIndex > toIndex) throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); l = list; //内容 offset = fromIndex; //开始下标 size = toIndex - fromIndex; //大小 this.modCount = l.modCount; } public E set(int index, E element) { rangeCheck(index);//检查下标是否越界 checkForComodification(); //检查并发异常 return l.set(index+offset, element); } public E get(int index) { rangeCheck(index); //是否越界 checkForComodification(); //并发异常 return l.get(index+offset); } public int size() { checkForComodification(); //并发异常 return size; } public void add(int index, E element) { rangeCheckForAdd(index); checkForComodification(); l.add(index+offset, element); this.modCount = l.modCount; size++; } public E remove(int index) { rangeCheck(index); checkForComodification(); E result = l.remove(index+offset); this.modCount = l.modCount; size--; return result; } protected void removeRange(int fromIndex, int toIndex) { checkForComodification(); l.removeRange(fromIndex+offset, toIndex+offset); this.modCount = l.modCount; size -= (toIndex-fromIndex); } public boolean addAll(Collection<? extends E> c) { return addAll(size, c); } public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index); int cSize = c.size(); if (cSize==0) return false; checkForComodification(); l.addAll(offset+index, c); this.modCount = l.modCount; size += cSize; return true; } public Iterator<E> iterator() { return listIterator(); } public ListIterator<E> listIterator(final int index) { checkForComodification(); rangeCheckForAdd(index); return new ListIterator<E>() { private final ListIterator<E> i = l.listIterator(index+offset); public boolean hasNext() { return nextIndex() < size; } public E next() { if (hasNext()) return i.next(); else throw new NoSuchElementException(); } public boolean hasPrevious() { return previousIndex() >= 0; } public E previous() { if (hasPrevious()) return i.previous(); else throw new NoSuchElementException(); } public int nextIndex() { return i.nextIndex() - offset; } public int previousIndex() { return i.previousIndex() - offset; } public void remove() { i.remove(); SubList.this.modCount = l.modCount; size--; } public void set(E e) { i.set(e); } public void add(E e) { i.add(e); SubList.this.modCount = l.modCount; size++; } }; } public List<E> subList(int fromIndex, int toIndex) { return new SubList<>(this, fromIndex, toIndex); } //越界异常 private void rangeCheck(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } //在添加元素时,越界异常,与上面不同的是参数不能等于本对象元素大小 private void rangeCheckForAdd(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } //越界文字提醒 private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } //这个方法是判断是否出现并发异常的方法,判断的依据就是两个modCount是否一致。 private void checkForComodification() { if (this.modCount != l.modCount) throw new ConcurrentModificationException(); } } 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); } }