📄 abstractlist.java
字号:
* lists have the same size, and all corresponding pairs of elements in * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null : * e1.equals(e2))</tt>.) In other words, two lists are defined to be * equal if they contain the same elements in the same order.<p> * * This implementation first checks if the specified object is this * list. If so, it returns <tt>true</tt>; if not, it checks if the * specified object is a list. If not, it returns <tt>false</tt>; if so, * it iterates over both lists, comparing corresponding pairs of elements. * If any comparison returns <tt>false</tt>, this method returns * <tt>false</tt>. If either iterator runs out of elements before the * other it returns <tt>false</tt> (as the lists are of unequal length); * otherwise it returns <tt>true</tt> when the iterations complete. * * @param o the object to be compared for equality with this list. * * @return <tt>true</tt> if the specified object is equal to this list. */ public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof List)) return false; ListIterator e1 = listIterator(); ListIterator e2 = ((List) o).listIterator(); while(e1.hasNext() && e2.hasNext()) { Object o1 = e1.next(); Object o2 = e2.next(); if (!(o1==null ? o2==null : o1.equals(o2))) return false; } return !(e1.hasNext() || e2.hasNext()); } /** * Returns the hash code value for this list. <p> * * This implementation uses exactly the code that is used to define the * list hash function in the documentation for the <tt>List.hashCode</tt> * method. * * @return the hash code value for this list. */ public int hashCode() { int hashCode = 1; Iterator i = iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); } return hashCode; } /** * Removes from this list all of the elements whose index is between * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. * Shifts any succeeding elements to the left (reduces their index). This * call shortens the ArrayList by <tt>(toIndex - fromIndex)</tt> * elements. (If <tt>toIndex==fromIndex</tt>, this operation has no * effect.)<p> * * This method is called by the <tt>clear</tt> operation on this list * and its subLists. Overriding this method to take advantage of * the internals of the list implementation can <i>substantially</i> * improve the performance of the <tt>clear</tt> operation on this list * and its subLists.<p> * * This implementation gets a list iterator positioned before * <tt>fromIndex</tt>, and repeatedly calls <tt>ListIterator.next</tt> * followed by <tt>ListIterator.remove</tt> until the entire range has * been removed. <b>Note: if <tt>ListIterator.remove</tt> requires linear * time, this implementation requires quadratic time.</b> * * @param fromIndex index of first element to be removed. * @param toIndex index after last element to be removed. */ protected void removeRange(int fromIndex, int toIndex) { ListIterator it = listIterator(fromIndex); for (int i=0, n=toIndex-fromIndex; i<n; i++) { it.next(); it.remove(); } } /** * The number of times this list has been <i>structurally modified</i>. * 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 field is used by the iterator and list iterator implementation * returned by the <tt>iterator</tt> and <tt>listIterator</tt> methods. * If the value of this field changes unexpectedly, the iterator (or list * iterator) will throw a <tt>ConcurrentModificationException</tt> in * response to the <tt>next</tt>, <tt>remove</tt>, <tt>previous</tt>, * <tt>set</tt> or <tt>add</tt> operations. This provides * <i>fail-fast</i> behavior, rather than non-deterministic behavior in * the face of concurrent modification during iteration.<p> * * <b>Use of this field by subclasses is optional.</b> If a subclass * wishes to provide fail-fast iterators (and list iterators), then it * merely has to increment this field in its <tt>add(int, Object)</tt> and * <tt>remove(int)</tt> methods (and any other methods that it overrides * that result in structural modifications to the list). A single call to * <tt>add(int, Object)</tt> or <tt>remove(int)</tt> must add no more than * one to this field, or the iterators (and list iterators) will throw * bogus <tt>ConcurrentModificationExceptions</tt>. If an implementation * does not wish to provide fail-fast iterators, this field may be * ignored. */ protected transient int modCount = 0;}class SubList extends AbstractList { private AbstractList l; private int offset; private int size; private int expectedModCount; SubList(AbstractList 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; expectedModCount = l.modCount; } public Object set(int index, Object element) { rangeCheck(index); checkForComodification(); return l.set(index+offset, element); } public Object get(int index) { rangeCheck(index); checkForComodification(); return l.get(index+offset); } public int size() { checkForComodification(); return size; } public void add(int index, Object element) { if (index<0 || index>size) throw new IndexOutOfBoundsException(); checkForComodification(); l.add(index+offset, element); expectedModCount = l.modCount; size++; modCount++; } public Object remove(int index) { rangeCheck(index); checkForComodification(); Object result = l.remove(index+offset); expectedModCount = l.modCount; size--; modCount++; return result; } protected void removeRange(int fromIndex, int toIndex) { checkForComodification(); l.removeRange(fromIndex+offset, toIndex+offset); expectedModCount = l.modCount; size -= (toIndex-fromIndex); modCount++; } public boolean addAll(Collection c) { return addAll(size, c); } public boolean addAll(int index, Collection c) { if (index<0 || index>size) throw new IndexOutOfBoundsException( "Index: "+index+", Size: "+size); int cSize = c.size(); if (cSize==0) return false; checkForComodification(); l.addAll(offset+index, c); expectedModCount = l.modCount; size += cSize; modCount++; return true; } public Iterator iterator() { return listIterator(); } public ListIterator listIterator(final int index) { checkForComodification(); if (index<0 || index>size) throw new IndexOutOfBoundsException( "Index: "+index+", Size: "+size); return new ListIterator() { private ListIterator i = l.listIterator(index+offset); public boolean hasNext() { return nextIndex() < size; } public Object next() { if (hasNext()) return i.next(); else throw new NoSuchElementException(); } public boolean hasPrevious() { return previousIndex() >= 0; } public Object 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(); expectedModCount = l.modCount; size--; modCount++; } public void set(Object o) { i.set(o); } public void add(Object o) { i.add(o); expectedModCount = l.modCount; size++; modCount++; } }; } public List subList(int fromIndex, int toIndex) { return new SubList(this, fromIndex, toIndex); } private void rangeCheck(int index) { if (index<0 || index>=size) throw new IndexOutOfBoundsException("Index: "+index+ ",Size: "+size); } private void checkForComodification() { if (l.modCount != expectedModCount) throw new ConcurrentModificationException(); }}class RandomAccessSubList extends SubList implements RandomAccess { RandomAccessSubList(AbstractList list, int fromIndex, int toIndex) { super(list, fromIndex, toIndex); } public List subList(int fromIndex, int toIndex) { return new RandomAccessSubList(this, fromIndex, toIndex); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -