迭代数据而不保存它

时间:2016-01-14 19:51:00

标签: java iterator

我想写一个简单的程序,它可以提供我从一些firstNumber开始打印n偶数的功能。它的数字是totalNumber。我不想保存它们,只需打印它们。这是我的一段代码:

import java.util.Iterator;

public class EvenNumbers implements Iterable<Integer>{

    private int firstNumber;
    private int totalNumbers;

    public EvenNumbers(int firstNumber, int totalNumbers) {
        this.firstNumber = firstNumber;
        this.totalNumbers = totalNumbers;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new myNewIterator();
    }

    private static class myNewIterator implements Iterator<Integer>{

        private int firstNumber;
        private int totalNumbers;
        private int tmp;

        public myNewIterator() {
            this.firstNumber = firstNumber;
            this.totalNumbers = totalNumbers;

            this.tmp = firstNumber - 2;
        }   

        @Override
        public boolean hasNext() {
            if(totalNumbers > 0){
                totalNumbers--;
                return true;
            }
            return false;
        }

        @Override
        public Integer next() {
            return tmp + 2;
        }
    }
}

主要:

public class Main {

    public static void main(String[] args) {
        EvenNumbers en = new EvenNumbers(14, 4);

        for (Integer n : en) {
            System.out.println(n);
        }

    }

}

您可以看到,我没有获得此计划的任何输出。 有人可以解释一下我做错了什么吗? 非常感谢!

2 个答案:

答案 0 :(得分:2)

为什么你有这么多代码?

public class Main {

    public static void main(String[] args) {
        int start = 14;
        int count = 4;

        for (int n = start; n < start + 2 * count; n += 2) {
            System.out.println(n);
        }
    }
}

答案 1 :(得分:1)

@fafl answer是一个更好,更简洁的答案。

指出此代码无效的原因:

<强> 1。问题在于myNewIterator构造函数。您正在为自己分配变量。此外,int的默认值为零,如果(totalNumbers&gt; 0)将始终失败,则迭代条件为。

public myNewIterator() {
        /** these two lines have to be changed**/ 
        this.firstNumber = firstNumber;
        this.totalNumbers = totalNumbers;
        /** end **/

        this.tmp = firstNumber - 2;
} 

您必须从构造函数中获取这两个值。以下是更正后的代码。我也纠正了构造函数名称。

<强> 2。你不能在hasNext()方法中减少totalNumbers,因为如果我调用hasNext()100次而没有调用next(),它应该只有一个下一个元素它应该仍然返回true,即它有下一个元素。所以当调用next()时应该发生减量。

第3。必须为每次next()调用更新tmp


这些更改也反映在以下代码中。

import java.util.Iterator;

public class EvenNumbers implements Iterable<Integer>{

private int firstNumber;
private int totalNumbers;

public EvenNumbers(int firstNumber, int totalNumbers) {
    this.firstNumber = firstNumber;
    this.totalNumbers = totalNumbers;
}

@Override
public Iterator<Integer> iterator() {
    /***** changed *****/ 
    return new myNewIterator(this.firstNumber,this.totalNumbers);
}

private static class myNewIterator implements Iterator<Integer>{

    private int firstNumber;
    private int totalNumbers;
    private int tmp;

    /***** changed *****/ 
    public myNewIterator(int firstNo,int totalNo) {
         /***** changed *****/
        /****     edited these lines     *******/
        this.firstNumber = firstNo;
        this.totalNumbers = totalNo;
        /***** ****/

        this.tmp = firstNumber - 2;
    }   

    @Override
    public boolean hasNext() {
        if(totalNumbers > 0){
            /***** changed *****/   
            //totalNumbers--; //commenting this line as repeated calls of this line makes this call unsafe
            return true;
        }
        return false;
    }

    @Override
    public Integer next() {
        /***** changed *****/  
        totalNumbers--;
        tmp = tmp + 2
        return tmp;
    }
}
}