橙沫沫 2015-07-13 23:43 采纳率: 33.3%
浏览 1598

java中的迭代器问题。

package testcc;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyArrrayList implements Iterable {
private static final int DEFAULT_CAPACITY = 10;
private int theSize;
private AnyType[] theItems;

public MyArrrayList() {
    clear();
}

public void clear() {
    theSize = 0;
    ensureCapality(DEFAULT_CAPACITY);
}

public int size() {
    return theSize;
}

public boolean isEmpty() {
    return size() == 0;
}

public void trimSize() {
    ensureCapality(size());
}

public AnyType get(int idx) {
    if (idx < 0 || idx >= size())
        throw new ArrayIndexOutOfBoundsException();
    return theItems[idx];
}

public AnyType set(int idx, AnyType newVal) {
    if (idx < 0 || idx >= size())
        throw new ArrayIndexOutOfBoundsException();
    AnyType old = theItems[idx];
    theItems[idx] = newVal;
    return old;
}

@SuppressWarnings("unchecked")
public void ensureCapality(int newCapality) {
    if (newCapality < theSize)
        return;
    AnyType[] old = theItems;
    theItems = (AnyType[]) new Object[newCapality];
    for (int i = 0; i < size(); i++) {
        theItems[i] = old[i];
    }
}

public boolean add(AnyType x) {
    add(size(), x);
    return true;
}

public void add(int idx, AnyType x) {
    if (theItems.length == size())
        ensureCapality(size() * 2 + 1);
    for (int i = theSize; i > idx; i--)
        theItems[i] = theItems[i - 1];
    theItems[idx] = x;
    theSize++;
}

public AnyType remove(int idx) {
    if (idx < 0 || idx >= size())
        throw new ArrayIndexOutOfBoundsException();
    AnyType removeItem = theItems[idx];
    for (int i = idx; i < theSize; i++)
        theItems[i] = theItems[i + 1];
    theSize--;
    return removeItem;
}

@Override
public Iterator<AnyType> iterator() {
    // TODO Auto-generated method stub
    return new ArrayListIterator(this);
}

private static class ArrayListIterator<AnyType> implements Iterator<AnyType> {
    private int current = 0;
    private MyArrrayList<AnyType> theList;
    public ArrayListIterator(MyArrrayList<AnyType> list){
        theList=list;
    }

    public boolean hasNext() {
        return current < theList.size();
    }

    public AnyType next() {

        return theList.theItems[current++];
    }

    public void remove() {
        theList.remove(--current);
    }
}

public static void main(String[] args) {
    MyArrrayList<Integer> list=new MyArrrayList<Integer>();
    for(int i=1;i<20;i++)
    list.add(i);

    Iterator<Integer> it=list.iterator();
    while(it.hasNext())
    {
        System.out.print(it.next());
        System.out.print(" ");

    }
    Iterator<Integer> its=list.iterator();


    while(it.hasNext())
    {
        System.out.println(it.next());
    }


}

}
为什么只有一次输出,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ;
二次迭代的输出无结果!求大神解答

  • 写回答

4条回答 默认 最新

  • miracle_yao 2015-07-13 23:49
    关注

    因为第一次迭代已经把“游标”移到最后了,hasnext都是返回空

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line