Example #1
0
 /**
  * Removes from this list all of the elements whose index is between {@code fromIndex}, inclusive,
  * and {@code toIndex}, exclusive. Shifts any succeeding elements to the left (reduces their
  * index). This call shortens the ArrayList by {@code (toIndex - fromIndex)} elements. (If {@code
  * toIndex==fromIndex}, this operation has no effect.)
  *
  * <p>This method is called by the {@code clear} 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 {@code clear} operation on this list and
  * its subLists.
  *
  * <p>This implementation gets a list iterator positioned before {@code fromIndex}, and repeatedly
  * calls {@code ListIterator.next} followed by {@code ListIterator.remove} until the entire range
  * has been removed. <b>Note: if {@code ListIterator.remove} 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<E> it = listIterator(fromIndex);
   for (int i = 0, n = toIndex - fromIndex; i < n; i++) {
     it.next();
     it.remove();
   }
 }
Example #2
0
 /**
  * {@inheritDoc}
  *
  * <p>This implementation first gets a list iterator that points to the end of the list (with
  * {@code listIterator(size())}). Then, it iterates backwards over the list until the specified
  * element is found, or the beginning of the list is reached.
  *
  * @throws ClassCastException {@inheritDoc}
  * @throws NullPointerException {@inheritDoc}
  */
 public int lastIndexOf(Object o) {
   ListIterator<E> e = listIterator(size());
   if (o == null) {
     while (e.hasPrevious()) if (e.previous() == null) return e.nextIndex();
   } else {
     while (e.hasPrevious()) if (o.equals(e.previous())) return e.nextIndex();
   }
   return -1;
 }
Example #3
0
  /**
   * Compares the specified object with this list for equality. Returns {@code true} if and only if
   * the specified object is also a list, both lists have the same size, and all corresponding pairs
   * of elements in the two lists are <i>equal</i>. (Two elements {@code e1} and {@code e2} are
   * <i>equal</i> if {@code (e1==null ? e2==null : e1.equals(e2))}.) 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
   * {@code true}; if not, it checks if the specified object is a list. If not, it returns {@code
   * false}; if so, it iterates over both lists, comparing corresponding pairs of elements. If any
   * comparison returns {@code false}, this method returns {@code false}. If either iterator runs
   * out of elements before the other it returns {@code false} (as the lists are of unequal length);
   * otherwise it returns {@code true} when the iterations complete.
   *
   * @param o the object to be compared for equality with this list
   * @return {@code true} 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<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());
  }