src/share/classes/java/util/AbstractList.java

Print this page




 158      * @throws IndexOutOfBoundsException     {@inheritDoc}
 159      */
 160     public E remove(int index) {
 161         throw new UnsupportedOperationException();
 162     }
 163 
 164 
 165     // Search Operations
 166 
 167     /**
 168      * {@inheritDoc}
 169      *
 170      * <p>This implementation first gets a list iterator (with
 171      * {@code listIterator()}).  Then, it iterates over the list until the
 172      * specified element is found or the end of the list is reached.
 173      *
 174      * @throws ClassCastException   {@inheritDoc}
 175      * @throws NullPointerException {@inheritDoc}
 176      */
 177     public int indexOf(Object o) {
 178         ListIterator<E> e = listIterator();
 179         if (o==null) {
 180             while (e.hasNext())
 181                 if (e.next()==null)
 182                     return e.previousIndex();
 183         } else {
 184             while (e.hasNext())
 185                 if (o.equals(e.next()))
 186                     return e.previousIndex();
 187         }
 188         return -1;
 189     }
 190 
 191     /**
 192      * {@inheritDoc}
 193      *
 194      * <p>This implementation first gets a list iterator that points to the end
 195      * of the list (with {@code listIterator(size())}).  Then, it iterates
 196      * backwards over the list until the specified element is found, or the
 197      * beginning of the list is reached.
 198      *
 199      * @throws ClassCastException   {@inheritDoc}
 200      * @throws NullPointerException {@inheritDoc}
 201      */
 202     public int lastIndexOf(Object o) {
 203         ListIterator<E> e = listIterator(size());
 204         if (o==null) {
 205             while (e.hasPrevious())
 206                 if (e.previous()==null)
 207                     return e.nextIndex();
 208         } else {
 209             while (e.hasPrevious())
 210                 if (o.equals(e.previous()))
 211                     return e.nextIndex();
 212         }
 213         return -1;
 214     }
 215 
 216 
 217     // Bulk Operations
 218 
 219     /**
 220      * Removes all of the elements from this list (optional operation).
 221      * The list will be empty after this call returns.
 222      *
 223      * <p>This implementation calls {@code removeRange(0, size())}.
 224      *
 225      * <p>Note that this implementation throws an
 226      * {@code UnsupportedOperationException} unless {@code remove(int
 227      * index)} or {@code removeRange(int fromIndex, int toIndex)} is
 228      * overridden.
 229      *
 230      * @throws UnsupportedOperationException if the {@code clear} operation
 231      *         is not supported by this list


 500      * This implementation first checks if the specified object is this
 501      * list. If so, it returns {@code true}; if not, it checks if the
 502      * specified object is a list. If not, it returns {@code false}; if so,
 503      * it iterates over both lists, comparing corresponding pairs of elements.
 504      * If any comparison returns {@code false}, this method returns
 505      * {@code false}.  If either iterator runs out of elements before the
 506      * other it returns {@code false} (as the lists are of unequal length);
 507      * otherwise it returns {@code true} when the iterations complete.
 508      *
 509      * @param o the object to be compared for equality with this list
 510      * @return {@code true} if the specified object is equal to this list
 511      */
 512     public boolean equals(Object o) {
 513         if (o == this)
 514             return true;
 515         if (!(o instanceof List))
 516             return false;
 517 
 518         ListIterator<E> e1 = listIterator();
 519         ListIterator e2 = ((List) o).listIterator();
 520         while(e1.hasNext() && e2.hasNext()) {
 521             E o1 = e1.next();
 522             Object o2 = e2.next();
 523             if (!(o1==null ? o2==null : o1.equals(o2)))
 524                 return false;
 525         }
 526         return !(e1.hasNext() || e2.hasNext());
 527     }
 528 
 529     /**
 530      * Returns the hash code value for this list.
 531      *
 532      * <p>This implementation uses exactly the code that is used to define the
 533      * list hash function in the documentation for the {@link List#hashCode}
 534      * method.
 535      *
 536      * @return the hash code value for this list
 537      */
 538     public int hashCode() {
 539         int hashCode = 1;
 540         for (E e : this)




 158      * @throws IndexOutOfBoundsException     {@inheritDoc}
 159      */
 160     public E remove(int index) {
 161         throw new UnsupportedOperationException();
 162     }
 163 
 164 
 165     // Search Operations
 166 
 167     /**
 168      * {@inheritDoc}
 169      *
 170      * <p>This implementation first gets a list iterator (with
 171      * {@code listIterator()}).  Then, it iterates over the list until the
 172      * specified element is found or the end of the list is reached.
 173      *
 174      * @throws ClassCastException   {@inheritDoc}
 175      * @throws NullPointerException {@inheritDoc}
 176      */
 177     public int indexOf(Object o) {
 178         ListIterator<E> it = listIterator();
 179         if (o==null) {
 180             while (it.hasNext())
 181                 if (it.next()==null)
 182                     return it.previousIndex();
 183         } else {
 184             while (it.hasNext())
 185                 if (o.equals(it.next()))
 186                     return it.previousIndex();
 187         }
 188         return -1;
 189     }
 190 
 191     /**
 192      * {@inheritDoc}
 193      *
 194      * <p>This implementation first gets a list iterator that points to the end
 195      * of the list (with {@code listIterator(size())}).  Then, it iterates
 196      * backwards over the list until the specified element is found, or the
 197      * beginning of the list is reached.
 198      *
 199      * @throws ClassCastException   {@inheritDoc}
 200      * @throws NullPointerException {@inheritDoc}
 201      */
 202     public int lastIndexOf(Object o) {
 203         ListIterator<E> it = listIterator(size());
 204         if (o==null) {
 205             while (it.hasPrevious())
 206                 if (it.previous()==null)
 207                     return it.nextIndex();
 208         } else {
 209             while (it.hasPrevious())
 210                 if (o.equals(it.previous()))
 211                     return it.nextIndex();
 212         }
 213         return -1;
 214     }
 215 
 216 
 217     // Bulk Operations
 218 
 219     /**
 220      * Removes all of the elements from this list (optional operation).
 221      * The list will be empty after this call returns.
 222      *
 223      * <p>This implementation calls {@code removeRange(0, size())}.
 224      *
 225      * <p>Note that this implementation throws an
 226      * {@code UnsupportedOperationException} unless {@code remove(int
 227      * index)} or {@code removeRange(int fromIndex, int toIndex)} is
 228      * overridden.
 229      *
 230      * @throws UnsupportedOperationException if the {@code clear} operation
 231      *         is not supported by this list


 500      * This implementation first checks if the specified object is this
 501      * list. If so, it returns {@code true}; if not, it checks if the
 502      * specified object is a list. If not, it returns {@code false}; if so,
 503      * it iterates over both lists, comparing corresponding pairs of elements.
 504      * If any comparison returns {@code false}, this method returns
 505      * {@code false}.  If either iterator runs out of elements before the
 506      * other it returns {@code false} (as the lists are of unequal length);
 507      * otherwise it returns {@code true} when the iterations complete.
 508      *
 509      * @param o the object to be compared for equality with this list
 510      * @return {@code true} if the specified object is equal to this list
 511      */
 512     public boolean equals(Object o) {
 513         if (o == this)
 514             return true;
 515         if (!(o instanceof List))
 516             return false;
 517 
 518         ListIterator<E> e1 = listIterator();
 519         ListIterator e2 = ((List) o).listIterator();
 520         while (e1.hasNext() && e2.hasNext()) {
 521             E o1 = e1.next();
 522             Object o2 = e2.next();
 523             if (!(o1==null ? o2==null : o1.equals(o2)))
 524                 return false;
 525         }
 526         return !(e1.hasNext() || e2.hasNext());
 527     }
 528 
 529     /**
 530      * Returns the hash code value for this list.
 531      *
 532      * <p>This implementation uses exactly the code that is used to define the
 533      * list hash function in the documentation for the {@link List#hashCode}
 534      * method.
 535      *
 536      * @return the hash code value for this list
 537      */
 538     public int hashCode() {
 539         int hashCode = 1;
 540         for (E e : this)