为什么用Iteator it = coll.iterator(); 会用,不理解
 1.因为Iterator 是接口 接口不能实例化  而coll对象中的 hasnext(),next()在API中并没有显示implements Itreator  也没有重写hasnext();next(),  
 2. ComperTo方法需要重写才能比较  为什么hasnext(),next()不需要重写呢
 3. 如果我自己定义个Arr类implements Iterator 我需要重写hasnext,next方法吗  next方法返回类型应该是什么
4.谢谢回答 iterator

解决方案 »

  1.   

    不要把iterator单单理解为一个接口,把它看成是一个容器,就很好理解了,实现Iterator肯定要重写方法的,next返回类型为Object
      

  2.   

    1.iterator()方法返回的是个迭代器,实现了Iterator接口,Iterator接口有hasnext(),next(),remove()三个方法,而Iterator接口与Collection没有任何父子关系。所以实现collection接口的类没有实现了以上的那三个iterator方法,实现这个方法的只是Iterator.三个方法在AbstractCollection里面重写了。2.如果是一个非抽象类,实现一个接口或者继承抽象类,此非抽象类,必须实现接口所有方法或者抽象类的所有抽象方法的.这是java的语法规定.3.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();
    }
        }