姜草茶 2015-07-13 23:48 采纳率: 100%
浏览 1770
已采纳

java迭代问题,二次迭代问题i

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
明明写了两个输出语句,为什么只有一个输出呢?

  • 写回答

7条回答

  • 丵鹰 2015-07-14 00:58
    关注

    Iterator its=list.iterator();

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

    第二个你参数写错了 应该是its.hasNext()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)