集合框架的集合基本都实现了iterator接口,为什么呢,这就是java里对类共性的提取,然后抽象,为什么要重载,是因为每种集合的迭代方式的都有区别,比如arraylist它的底层是数组,那么具体迭代的方式就应该对数组进行,linkedlist 也有迭代器,它的底层是链表,那么具体的迭代就应该对链表进行。
至于第二个问题没有具体去看过,但是原理基本都一样,为了实现代码的重用,肯定abstractlist里面有已经实现的现成的方法,arraylist继承以后就不需要在重写相同功能的代码了,我估计是好多,至于为什么要实现list,原因是abstactlist毕竟只是实现了list的部分方法,其他方法还得在arraylist中具体实现, 题外话,至于为什么要抽相出一层abstractlist,原因是实现了list接口的类,发现有好几个类中方法中代码,那么就有必要再抽象一层,把共有的方法先实现了,各自具有的都有的方法,放在各自类中实现,可以理解为提高代码重用性,这样写也容易扩展……

解决方案 »

  1.   

    竟然没权限编辑,那就在这里说了。
    1.ListIterator中继承了Iterator。为什么还要将Iterator的方法重载一边呢?
    ListIterator是一个interface,Iterator也是个interface,我知道这种继承有他的意义,但是看jdk
    他在ListIterator里把Iterator的方法又override一遍,这里的用途是什么呢
    2.ArrayLIst为什么在extends AbstractList之后还要去 implements list呢?请看jdkpublic class ArrayList<E> extends AbstractList<E>
            implements List<E>, RandomAccess, Cloneable, java.io.SerializableArrayList<E>为什么要再次implements List<E>,如果说要实现里面的方法,这不和override AbstractList的效果一样么,因为AbstractList也是implement list的。以上两个疑惑可能是我对java基础的理解有误或者jdk这么写确实有他自己的意图,请大家指点
      

  2.   

    第一个问题:没懂你这个“重载”的意思,
    /*
     * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     */package java.util;/**
     * An iterator for lists that allows the programmer
     * to traverse the list in either direction, modify
     * the list during iteration, and obtain the iterator's
     * current position in the list. A {@code ListIterator}
     * has no current element; its <I>cursor position</I> always
     * lies between the element that would be returned by a call
     * to {@code previous()} and the element that would be
     * returned by a call to {@code next()}.
     * An iterator for a list of length {@code n} has {@code n+1} possible
     * cursor positions, as illustrated by the carets ({@code ^}) below:
     * <PRE>
     *                      Element(0)   Element(1)   Element(2)   ... Element(n-1)
     * cursor positions:  ^            ^            ^            ^                  ^
     * </PRE>
     * Note that the {@link #remove} and {@link #set(Object)} methods are
     * <i>not</i> defined in terms of the cursor position;  they are defined to
     * operate on the last element returned by a call to {@link #next} or
     * {@link #previous()}.
     *
     * <p>This interface is a member of the
     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
     * Java Collections Framework</a>.
     *
     * @author  Josh Bloch
     * @see Collection
     * @see List
     * @see Iterator
     * @see Enumeration
     * @see List#listIterator()
     * @since   1.2
     */
    public interface ListIterator<E> extends Iterator<E> {
        // Query Operations    /**
         * Returns {@code true} if this list iterator has more elements when
         * traversing the list in the forward direction. (In other words,
         * returns {@code true} if {@link #next} would return an element rather
         * than throwing an exception.)
         *
         * @return {@code true} if the list iterator has more elements when
         *         traversing the list in the forward direction
         */
        boolean hasNext();    /**
         * Returns the next element in the list and advances the cursor position.
         * This method may be called repeatedly to iterate through the list,
         * or intermixed with calls to {@link #previous} to go back and forth.
         * (Note that alternating calls to {@code next} and {@code previous}
         * will return the same element repeatedly.)
         *
         * @return the next element in the list
         * @throws NoSuchElementException if the iteration has no next element
         */
        E next();    /**
         * Returns {@code true} if this list iterator has more elements when
         * traversing the list in the reverse direction.  (In other words,
         * returns {@code true} if {@link #previous} would return an element
         * rather than throwing an exception.)
         *
         * @return {@code true} if the list iterator has more elements when
         *         traversing the list in the reverse direction
         */
        boolean hasPrevious();    /**
         * Returns the previous element in the list and moves the cursor
         * position backwards.  This method may be called repeatedly to
         * iterate through the list backwards, or intermixed with calls to
         * {@link #next} to go back and forth.  (Note that alternating calls
         * to {@code next} and {@code previous} will return the same
         * element repeatedly.)
         *
         * @return the previous element in the list
         * @throws NoSuchElementException if the iteration has no previous
         *         element
         */
        E previous();    /**
         * Returns the index of the element that would be returned by a
         * subsequent call to {@link #next}. (Returns list size if the list
         * iterator is at the end of the list.)
         *
         * @return the index of the element that would be returned by a
         *         subsequent call to {@code next}, or list size if the list
         *         iterator is at the end of the list
         */
        int nextIndex();    /**
         * Returns the index of the element that would be returned by a
         * subsequent call to {@link #previous}. (Returns -1 if the list
         * iterator is at the beginning of the list.)
         *
         * @return the index of the element that would be returned by a
         *         subsequent call to {@code previous}, or -1 if the list
         *         iterator is at the beginning of the list
         */
        int previousIndex();
        // Modification Operations    /**
         * Removes from the list the last element that was returned by {@link
         * #next} or {@link #previous} (optional operation).  This call can
         * only be made once per call to {@code next} or {@code previous}.
         * It can be made only if {@link #add} has not been
         * called after the last call to {@code next} or {@code previous}.
         *
         * @throws UnsupportedOperationException if the {@code remove}
         *         operation is not supported by this list iterator
         * @throws IllegalStateException if neither {@code next} nor
         *         {@code previous} have been called, or {@code remove} or
         *         {@code add} have been called after the last call to
         *         {@code next} or {@code previous}
         */
        void remove();    /**
         * Replaces the last element returned by {@link #next} or
         * {@link #previous} with the specified element (optional operation).
         * This call can be made only if neither {@link #remove} nor {@link
         * #add} have been called after the last call to {@code next} or
         * {@code previous}.
         *
         * @param e the element with which to replace the last element returned by
         *          {@code next} or {@code previous}
         * @throws UnsupportedOperationException if the {@code set} operation
         *         is not supported by this list iterator
         * @throws ClassCastException if the class of the specified element
         *         prevents it from being added to this list
         * @throws IllegalArgumentException if some aspect of the specified
         *         element prevents it from being added to this list
         * @throws IllegalStateException if neither {@code next} nor
         *         {@code previous} have been called, or {@code remove} or
         *         {@code add} have been called after the last call to
         *         {@code next} or {@code previous}
         */
        void set(E e);    /**
         * Inserts the specified element into the list (optional operation).
         * The element is inserted immediately before the element that
         * would be returned by {@link #next}, if any, and after the element
         * that would be returned by {@link #previous}, if any.  (If the
         * list contains no elements, the new element becomes the sole element
         * on the list.)  The new element is inserted before the implicit
         * cursor: a subsequent call to {@code next} would be unaffected, and a
         * subsequent call to {@code previous} would return the new element.
         * (This call increases by one the value that would be returned by a
         * call to {@code nextIndex} or {@code previousIndex}.)
         *
         * @param e the element to insert
         * @throws UnsupportedOperationException if the {@code add} method is
         *         not supported by this list iterator
         * @throws ClassCastException if the class of the specified element
         *         prevents it from being added to this list
         * @throws IllegalArgumentException if some aspect of this element
         *         prevents it from being added to this list
         */
        void add(E e);
    }这里好像没有你说的 重写override,我这是jdk7.0
    第二个问题:  我上面已经说的很清楚了,这个AbstractList是 对部分类(实现了List接口)的共性的抽取,你可以看API看看它的子类(AbstractSequentialList, ArrayList, Vector ), 至于你说的为什么继承了AbstractList还要实现List,个人愚见,我认为是方便扩展,。。
      

  3.   

    第一个问题,是我陈述的有问题,应该是“实现”,非override
    比如说list中方法:
    boolean hasNext();
    为什么还要在ListIterator中实现这个list中的方法呢?我的理解是,在ListIterator只需要将自己扩展出来的方法定义下,无需再把list原有的方法声明一遍。因为一个具体类要实现ListIterator,它必然也要求去实现list中的方法。
    即list中的方法不需要在ListIterator中重新implement一遍,它只需要定义自己特有的方法。也许我的理解有误,请指正哥 你的扣扣是多少,交个朋友吧……,也便于讨论下问题