📄 abstractlist.java
字号:
return listIterator(0); } /** * Obtain a ListIterator over this list, starting at a given position. * A first call to next() would return the same as get(index), and a * first call to previous() would return the same as get(index - 1). * <p> * * This implementation uses size(), get(int), set(int, Object), * add(int, Object), and remove(int) of the backing list, and does not * support remove, set, or add unless the list does. This implementation * is fail-fast if you correctly maintain modCount. * * @param index the position, between 0 and size() inclusive, to begin the * iteration from * @return a ListIterator over the elements of this list, in order, starting * at index * @throws IndexOutOfBoundsException if index < 0 || index > size() * @see #modCount */ public ListIterator listIterator(final int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size()); return new ListIterator() { private int knownMod = modCount; private int position = index; private int lastReturned = -1; private int size = size(); // This will get inlined, since it is private. /** * Checks for modifications made to the list from * elsewhere while iteration is in progress. * * @throws ConcurrentModificationException if the * list has been modified elsewhere. */ private void checkMod() { if (knownMod != modCount) throw new ConcurrentModificationException(); } /** * Tests to see if there are any more objects to * return. * * @return True if the end of the list has not yet been * reached. * @throws ConcurrentModificationException if the * list has been modified elsewhere. */ public boolean hasNext() { checkMod(); return position < size; } /** * Tests to see if there are objects prior to the * current position in the list. * * @return True if objects exist prior to the current * position of the iterator. * @throws ConcurrentModificationException if the * list has been modified elsewhere. */ public boolean hasPrevious() { checkMod(); return position > 0; } /** * Retrieves the next object from the list. * * @return The next object. * @throws NoSuchElementException if there are no * more objects to retrieve. * @throws ConcurrentModificationException if the * list has been modified elsewhere. */ public Object next() { checkMod(); if (position == size) throw new NoSuchElementException(); lastReturned = position; return get(position++); } /** * Retrieves the previous object from the list. * * @return The next object. * @throws NoSuchElementException if there are no * previous objects to retrieve. * @throws ConcurrentModificationException if the * list has been modified elsewhere. */ public Object previous() { checkMod(); if (position == 0) throw new NoSuchElementException(); lastReturned = --position; return get(lastReturned); } /** * Returns the index of the next element in the * list, which will be retrieved by <code>next()</code> * * @return The index of the next element. * @throws ConcurrentModificationException if the list * has been modified elsewhere. */ public int nextIndex() { checkMod(); return position; } /** * Returns the index of the previous element in the * list, which will be retrieved by <code>previous()</code> * * @return The index of the previous element. * @throws ConcurrentModificationException if the list * has been modified elsewhere. */ public int previousIndex() { checkMod(); return position - 1; } /** * Removes the last object retrieved by <code>next()</code> * or <code>previous()</code> from the list, if the list * supports object removal. * * @throws IllegalStateException if the iterator is positioned * before the start of the list or the last object has already * been removed. * @throws UnsupportedOperationException if the list does * not support removing elements. * @throws ConcurrentModificationException if the list * has been modified elsewhere. */ public void remove() { checkMod(); if (lastReturned < 0) throw new IllegalStateException(); AbstractList.this.remove(lastReturned); size--; position = lastReturned; lastReturned = -1; knownMod = modCount; } /** * Replaces the last object retrieved by <code>next()</code> * or <code>previous</code> with o, if the list supports object * replacement and an add or remove operation has not already * been performed. * * @throws IllegalStateException if the iterator is positioned * before the start of the list or the last object has already * been removed. * @throws UnsupportedOperationException if the list doesn't support * the addition or removal of elements. * @throws ClassCastException if the type of o is not a valid type * for this list. * @throws IllegalArgumentException if something else related to o * prevents its addition. * @throws ConcurrentModificationException if the list * has been modified elsewhere. */ public void set(Object o) { checkMod(); if (lastReturned < 0) throw new IllegalStateException(); AbstractList.this.set(lastReturned, o); } /** * Adds the supplied object before the element that would be returned * by a call to <code>next()</code>, if the list supports addition. * * @param o The object to add to the list. * @throws UnsupportedOperationException if the list doesn't support * the addition of new elements. * @throws ClassCastException if the type of o is not a valid type * for this list. * @throws IllegalArgumentException if something else related to o * prevents its addition. * @throws ConcurrentModificationException if the list * has been modified elsewhere. */ public void add(Object o) { checkMod(); AbstractList.this.add(position++, o); size++; lastReturned = -1; knownMod = modCount; } }; } /** * Remove the element at a given position in this list (optional operation). * Shifts all remaining elements to the left to fill the gap. This * implementation always throws an UnsupportedOperationException. * If you want fail-fast iterators, be sure to increment modCount when * overriding this. * * @param index the position within the list of the object to remove * @return the object that was removed * @throws UnsupportedOperationException if this list does not support the * remove operation * @throws IndexOutOfBoundsException if index < 0 || index >= size() * @see #modCount */ public Object remove(int index) { throw new UnsupportedOperationException(); } /** * Remove a subsection of the list. This is called by the clear and * removeRange methods of the class which implements subList, which are * difficult for subclasses to override directly. Therefore, this method * should be overridden instead by the more efficient implementation, if one * exists. Overriding this can reduce quadratic efforts to constant time * in some cases! * <p> * * This implementation first checks for illegal or out of range arguments. It * then obtains a ListIterator over the list using listIterator(fromIndex). * It then calls next() and remove() on this iterator repeatedly, toIndex - * fromIndex times. * * @param fromIndex the index, inclusive, to remove from. * @param toIndex the index, exclusive, to remove to. * @throws UnsupportedOperationException if the list does * not support removing elements. */ protected void removeRange(int fromIndex, int toIndex) { ListIterator itr = listIterator(fromIndex); for (int index = fromIndex; index < toIndex; index++) { itr.next(); itr.remove(); } } /** * Replace an element of this list with another object (optional operation). * This implementation always throws an UnsupportedOperationException. * * @param index the position within this list of the element to be replaced * @param o the object to replace it with * @return the object that was replaced * @throws UnsupportedOperationException if this list does not support the * set operation * @throws IndexOutOfBoundsException if index < 0 || index >= size() * @throws ClassCastException if o cannot be added to this list due to its * type * @throws IllegalArgumentException if o cannot be added to this list for * some other reason */ public Object set(int index, Object o) { throw new UnsupportedOperationException(); } /** * Obtain a List view of a subsection of this list, from fromIndex * (inclusive) to toIndex (exclusive). If the two indices are equal, the * sublist is empty. The returned list should be modifiable if and only * if this list is modifiable. Changes to the returned list should be * reflected in this list. If this list is structurally modified in * any way other than through the returned list, the result of any subsequent * operations on the returned list is undefined. * <p> * * This implementation returns a subclass of AbstractList. It stores, in * private fields, the offset and size of the sublist, and the expected * modCount of the backing list. If the backing list implements RandomAccess, * the sublist will also. * <p> * * The subclass's <code>set(int, Object)</code>, <code>get(int)</code>, * <code>add(int, Object)</code>, <code>remove(int)</code>, * <code>addAll(int, Collection)</code> and * <code>removeRange(int, int)</code> methods all delegate to the * corresponding methods on the backing abstract list, after * bounds-checking the index and adjusting for the offset. The * <code>addAll(Collection c)</code> method merely returns addAll(size, c). * The <code>listIterator(int)</code> method returns a "wrapper object" * over a list iterator on the backing list, which is created with the * corresponding method on the backing list. The <code>iterator()</code> * method merely returns listIterator(), and the <code>size()</code> method * merely returns the subclass's size field. * <p> * * All methods first check to see if the actual modCount of the backing * list is equal to its expected value, and throw a * ConcurrentModificationException if it is not. * * @param fromIndex the index that the returned list should start from * (inclusive) * @param toIndex the index that the returned list should go to (exclusive) * @return a List backed by a subsection of this list * @throws IndexOutOfBoundsException if fromIndex < 0 * || toIndex > size() * @throws IllegalArgumentException if fromIndex > toIndex * @see ConcurrentModificationException * @see RandomAccess */ public List subList(int fromIndex, int toIndex) { // This follows the specification of AbstractList, but is inconsistent // with the one in List. Don't you love Sun's inconsistencies? if (fromIndex > toIndex) throw new IllegalArgumentException(fromIndex + " > " + toIndex); if (fromIndex < 0 || toIndex > size()) throw new IndexOutOfBoundsException(); if (this instanceof RandomAccess) return new RandomAccessSubList(this, fromIndex, toIndex); return new SubList(this, fromIndex, toIndex); } /** * This class follows the implementation requirements set forth in * {@link AbstractList#subList(int, int)}. It matches Sun's implementation * by using a non-public top-level class in the same package. * * @author Original author unknown * @author Eric Blake (ebb9@email.byu.edu) */ private static class SubList extends AbstractList { // Package visible, for use by iterator. /** The original list. */ final AbstractList backingList; /** The index of the first element of the sublist. */ final int offset; /** The size of the sublist. */ int size; /** * Construct the sublist. * * @param backing the list this comes from * @param fromIndex the lower bound, inclusive * @param toIndex the upper bound, exclusive */ SubList(AbstractList backing, int fromIndex, int toIndex) { backingList = backing; modCount = backing.modCount; offset = fromIndex; size = toIndex - fromIndex; } /** * This method checks the two modCount fields to ensure that there has * not been a concurrent modification, returning if all is okay. * * @throws ConcurrentModificationException if the backing list has been * modified externally to this sublist */ // This can be inlined. Package visible, for use by iterator. void checkMod() { if (modCount != backingList.modCount) throw new ConcurrentModificationException(); } /** * This method checks that a value is between 0 and size (inclusive). If * it is not, an exception is thrown. * * @param index the value to check * @throws IndexOutOfBoundsException if index < 0 || index > size() */ // This will get inlined, since it is private. private void checkBoundsInclusive(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); } /** * This method checks that a value is between 0 (inclusive) and size * (exclusive). If it is not, an exception is thrown.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -