📄 abstractlist.java
字号:
* collection prevents it from being added to this List. * * @throws IllegalArgumentException some aspect an element of the * specified collection prevents it from being added to this * List. * * @throws IndexOutOfBoundsException index out of range (<tt>index < 0 * || index > size()</tt>). * * @throws NullPointerException if the specified collection is null. */ public boolean addAll(int index, Collection c) { boolean modified = false; Iterator e = c.iterator(); while (e.hasNext()) { add(index++, e.next()); modified = true; } return modified; } // Iterators /** * Returns an iterator over the elements in this list in proper * sequence. <p> * * This implementation returns a straightforward implementation of the * iterator interface, relying on the backing list's <tt>size()</tt>, * <tt>get(int)</tt>, and <tt>remove(int)</tt> methods.<p> * * Note that the iterator returned by this method will throw an * <tt>UnsupportedOperationException</tt> in response to its * <tt>remove</tt> method unless the list's <tt>remove(int)</tt> method is * overridden.<p> * * This implementation can be made to throw runtime exceptions in the face * of concurrent modification, as described in the specification for the * (protected) <tt>modCount</tt> field. * * @return an iterator over the elements in this list in proper sequence. * * @see #modCount */ public Iterator iterator() { return new Itr(); } /** * Returns an iterator of the elements in this list (in proper sequence). * This implementation returns <tt>listIterator(0)</tt>. * * @return an iterator of the elements in this list (in proper sequence). * * @see #listIterator(int) */ public ListIterator listIterator() { return listIterator(0); } /** * Returns a list iterator of the elements in this list (in proper * sequence), starting at the specified position in the list. The * specified index indicates the first element that would be returned by * an initial call to the <tt>next</tt> method. An initial call to * the <tt>previous</tt> method would return the element with the * specified index minus one.<p> * * This implementation returns a straightforward implementation of the * <tt>ListIterator</tt> interface that extends the implementation of the * <tt>Iterator</tt> interface returned by the <tt>iterator()</tt> method. * The <tt>ListIterator</tt> implementation relies on the backing list's * <tt>get(int)</tt>, <tt>set(int, Object)</tt>, <tt>add(int, Object)</tt> * and <tt>remove(int)</tt> methods.<p> * * Note that the list iterator returned by this implementation will throw * an <tt>UnsupportedOperationException</tt> in response to its * <tt>remove</tt>, <tt>set</tt> and <tt>add</tt> methods unless the * list's <tt>remove(int)</tt>, <tt>set(int, Object)</tt>, and * <tt>add(int, Object)</tt> methods are overridden.<p> * * This implementation can be made to throw runtime exceptions in the * face of concurrent modification, as described in the specification for * the (protected) <tt>modCount</tt> field. * * @param index index of the first element to be returned from the list * iterator (by a call to the <tt>next</tt> method). * * @return a list iterator of the elements in this list (in proper * sequence), starting at the specified position in the list. * * @throws IndexOutOfBoundsException if the specified index is out of * range (<tt>index < 0 || index > size()</tt>). * * @see #modCount */ public ListIterator listIterator(final int index) { if (index<0 || index>size()) throw new IndexOutOfBoundsException("Index: "+index); return new ListItr(index); } private class Itr implements Iterator { /** * 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 Object next() { checkForComodification(); try { Object next = get(cursor); lastRet = cursor++; return next; } catch(IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } public void remove() { if (lastRet == -1) 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 { ListItr(int index) { cursor = index; } public boolean hasPrevious() { return cursor != 0; } public Object previous() { checkForComodification(); try { int i = cursor - 1; Object 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(Object o) { if (lastRet == -1) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.set(lastRet, o); expectedModCount = modCount; } catch(IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } public void add(Object o) { checkForComodification(); try { AbstractList.this.add(cursor++, o); lastRet = -1; expectedModCount = modCount; } catch(IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } } /** * Returns a view of the portion of this list between <tt>fromIndex</tt>, * inclusive, and <tt>toIndex</tt>, exclusive. (If <tt>fromIndex</tt> and * <tt>toIndex</tt> are equal, the returned list is empty.) The returned * list is backed by this list, so changes in the returned list are * reflected in this list, and vice-versa. The returned list supports all * of the optional list operations supported by this list.<p> * * This method eliminates the need for explicit range operations (of the * sort that commonly exist for arrays). Any operation that expects a * list can be used as a range operation by operating on a subList view * instead of a whole list. For example, the following idiom removes a * range of elements from a list: * <pre> * list.subList(from, to).clear(); * </pre> * Similar idioms may be constructed for <tt>indexOf</tt> and * <tt>lastIndexOf</tt>, and all of the algorithms in the * <tt>Collections</tt> class can be applied to a subList.<p> * * The semantics of the list returned by this method become undefined if * the backing list (i.e., this list) is <i>structurally modified</i> in * any way other than via the returned list. (Structural modifications are * those that change the size of the list, or otherwise perturb it in such * a fashion that iterations in progress may yield incorrect results.)<p> * * This implementation returns a list that subclasses * <tt>AbstractList</tt>. The subclass stores, in private fields, the * offset of the subList within the backing list, the size of the subList * (which can change over its lifetime), and the expected * <tt>modCount</tt> value of the backing list. There are two variants * of the subclass, one of which implements <tt>RandomAccess</tt>. * If this list implements <tt>RandomAccess</tt> the returned list will * be an instance of the subclass that implements <tt>RandomAccess</tt>.<p> * * The subclass's <tt>set(int, Object)</tt>, <tt>get(int)</tt>, * <tt>add(int, Object)</tt>, <tt>remove(int)</tt>, <tt>addAll(int, * Collection)</tt> and <tt>removeRange(int, int)</tt> methods all * delegate to the corresponding methods on the backing abstract list, * after bounds-checking the index and adjusting for the offset. The * <tt>addAll(Collection c)</tt> method merely returns <tt>addAll(size, * c)</tt>.<p> * * The <tt>listIterator(int)</tt> 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 <tt>iterator</tt> method * merely returns <tt>listIterator()</tt>, and the <tt>size</tt> method * merely returns the subclass's <tt>size</tt> field.<p> * * All methods first check to see if the actual <tt>modCount</tt> of the * backing list is equal to its expected value, and throw a * <tt>ConcurrentModificationException</tt> if it is not. * * @param fromIndex low endpoint (inclusive) of the subList. * @param toIndex high endpoint (exclusive) of the subList. * @return a view of the specified range within this list. * @throws IndexOutOfBoundsException endpoint index value out of range * <tt>(fromIndex < 0 || toIndex > size)</tt> * @throws IllegalArgumentException endpoint indices out of order * <tt>(fromIndex > toIndex)</tt> */ public List subList(int fromIndex, int toIndex) { return (this instanceof RandomAccess ? new RandomAccessSubList(this, fromIndex, toIndex) : new SubList(this, fromIndex, toIndex)); } // Comparison and hashing /** * Compares the specified object with this list for equality. Returns * <tt>true</tt> if and only if the specified object is also a list, both
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -