如题,看了网上的一些回答,感觉还是不清楚。比如写下面的代码:...List l = new ArrayList();Iterator i = l.iterator();while(i.hasNext()){...}...既然Iterator是接口,那么它的hasNext() 方法应该是抽象方法才对,到底在哪里被重写过呢???请教高人详细指点

解决方案 »

  1.   

    ArrayList 继承了 AbstractList
    在AbstractList里面AbstractList.class:    public Iterator<E> iterator() {
    return new Itr();
        }    private class Itr implements Iterator<E> {
    /**
     * 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 E next() {
                checkForComodification();
        try {
    E 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();
    }
        }
    具体的代码 你可以看看java的源码
      

  2.   

    虽然Iterator是接口,但是.iterator方法返回的却是实现了该接口的一个对象。
    hasNext()方法就在这个对象所属的真实类中(虽然通常我们并不关心它)被实现的。
      

  3.   

    2楼说的没错,楼主既然说了接口中的方法是抽象的,也就是没有实现的,但是我们调用Iterator是这样写的Iterator i = l.iterator(); 也就是说并不是通过Iterator的实现类去定义这个i(例如在WindowEvent中需要用windowclosing 方法不是直接实现WindowListener接口而是去继承WindowAdapter),这里说的只是让lz明白如果要用接口中的方法必须找到一个实现此接口的类的对象去调用方法,而Iterator并没有自己写个类去实现,所以像2楼说的返回一个对象~~~  个人见解。
      

  4.   

    如楼上所说,一般不关心Iterator接口的实现,而且Iterator接口的实现也不止一个。能返回Iterator对象的都实现了Iterator接口就可以了。
      

  5.   

    AbstractList.class:    public Iterator<E> iterator() {
        return new Itr();
        }    private class Itr implements Iterator<E> {
        /**
         * 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 E next() {
                checkForComodification();
            try {
            E 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();
        }
        }