Java 迭代器返回错误的整数值

Java 迭代器返回错误的整数值,java,iterator,integer,iterable,Java,Iterator,Integer,Iterable,我必须实现classIncrementer,它应该实现Iterable 输出应为: 1 2 3 4 5 6 7 8 9 10 1 3 5 7 9 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1 1 2 3 4 6 8 10 1 2 3 4 5 6 7 8 6 4 2 10 9 8 7 6 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 3 5 7 9 11 9 8 7 6 5 4 3 2 1 2 3 4 6 8 10 2

我必须实现class
Incrementer
,它应该实现
Iterable

输出应为:

1 2 3 4 5 6 7 8 9 10 
1 3 5 7 9 
10 9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 1 
1 2 3 4 6 8 10 
1 2 3 4 5 6 7 8 6 4 2 
10 9 8 7 6 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 
3 5 7 9 11 
9 8 7 6 5 4 3 2 1 

2 3 4 6 8 10 
2 3 4 5 6 7 8 
9 8 7 6 5 6 7 8 9 10 
我确实得到了:

1 2 3 4 5 6 7 8 9 10 
1 3 5 7 9 
10 9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 1 
1 2 3 4 6 8 10 
1 2 3 4 5 6 7 8 6 4 2 
10 9 8 7 6 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 
3 5 7 9 11 
9 8 7 6 5 4 3 2 1 

2 3 4 6 8 10 
2 3 4 5 6 7 8 
9 8 7 6 5 6 7 8 9 10 
我的
Incrementer
类如下所示:

    package in;

import java.util.Iterator;

public class Incrementer implements Iterable<Integer> {
    int val, step, a, b;

    private Incrementer(int a, int b, int step) {
        this.step = step;
        this.a = a;
        this.b = b;
        if (step > 0)
            val = a;
        else
            val = b;
    }

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

            @Override
            public boolean hasNext() {
                if (step < 0 && val > a)
                    return true;
                else if (step > 0 && val < b)
                    return true;
                return false;
            }

            @Override
            public Integer next() {
                return val += step;
            }

            @Override
            public void remove() {
            }
        };
    }

    public static Incrementer in(int a, int b) {
        ///tu zmieniamy tresc dla ostatniego przypadku

        if (a < b)
            return new Incrementer(a, b, 1);
        else
            return new Incrementer(b, a, -1);
    }

    public Incrementer by(int step) {
        this.step = step;
        if (this.step < 0 && this.a < this.b || this.step > 0 && this.a > this.b) {
            int tmp = this.a;
            this.a = this.b;
            this.b = tmp;
        }
        return this;
    }

}

我不知道我犯了什么错误。

错误在于您没有初始化
val
,因此它将从
0
(默认值)开始

在第二个示例中,您将
返回val+=step,具有
val=0
step=2
,因此它将从
2
开始并从那里继续

在第三个示例中,
a=10
b=1
step=-1
val=0
,因此您将不会在

if (step < 0 && val > a)
您还应该修改
hasNext()
中的条件:

您还可以测试相反的情况:

for(int k : in(10, 1).by(1)) System.out.print(k + " ");

以下是完整的代码:

public class Incrementer implements Iterable<Integer> {
    int val, step, a, b;

    private Incrementer(int a, int b, int step) {
        this.step = step;
        this.a = a;
        this.b = b;
        if (step > 0)
            val = a;
        else
            val = b;
    }

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

            @Override
            public boolean hasNext() {
                if (step < 0 && val >= a)
                    return true;
                else if (step > 0 && val <= b)
                    return true;
                return false;
            }

            @Override
            public Integer next() {
                int ret = val;
                val += step;
                return ret;
            }

            @Override
            public void remove() {
            }
        };
    }

    public static Incrementer in(int a, int b) {
        ///tu zmieniamy tresc dla ostatniego przypadku

        if (a < b)
            return new Incrementer(a, b, 1);
        else
            return new Incrementer(b, a, -1);
    }

    public Incrementer by(int step) {
        if ((this.step<0)!=(step<0) && this.val==this.a)
            this.val = this.b;
        else if ((this.step<0)!=(step<0) && this.val==this.b)
            this.val = this.a;
        else if (this.val!=this.a && this.val!=this.b) {
            this.val -= this.step;
            this.val += step;
        }
        this.step = step;

        return this;
    }

}
公共类递增器实现Iterable{
int val,步骤a,b;
专用递增器(整数a、整数b、整数步长){
这个步骤=步骤;
这个a=a;
这个.b=b;
如果(步骤>0)
val=a;
其他的
val=b;
}
@凌驾
公共迭代器迭代器(){
返回新的迭代器(){
@凌驾
公共布尔hasNext(){
如果(步骤<0&&val>=a)
返回true;

否则,如果(步骤>0&&val错误是您没有初始化
val
,那么它将从
0
(默认值)开始

在第二个示例中,您将
返回val+=step;
,其中
val=0
step=2
,因此它将从
2
开始并从那里继续

在第三个示例中,
a=10
b=1
step=-1
val=0
,因此您将不会在

if (step < 0 && val > a)
您还应该修改
hasNext()
中的条件:

您还可以测试相反的情况:

for(int k : in(10, 1).by(1)) System.out.print(k + " ");

以下是完整的代码:

public class Incrementer implements Iterable<Integer> {
    int val, step, a, b;

    private Incrementer(int a, int b, int step) {
        this.step = step;
        this.a = a;
        this.b = b;
        if (step > 0)
            val = a;
        else
            val = b;
    }

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

            @Override
            public boolean hasNext() {
                if (step < 0 && val >= a)
                    return true;
                else if (step > 0 && val <= b)
                    return true;
                return false;
            }

            @Override
            public Integer next() {
                int ret = val;
                val += step;
                return ret;
            }

            @Override
            public void remove() {
            }
        };
    }

    public static Incrementer in(int a, int b) {
        ///tu zmieniamy tresc dla ostatniego przypadku

        if (a < b)
            return new Incrementer(a, b, 1);
        else
            return new Incrementer(b, a, -1);
    }

    public Incrementer by(int step) {
        if ((this.step<0)!=(step<0) && this.val==this.a)
            this.val = this.b;
        else if ((this.step<0)!=(step<0) && this.val==this.b)
            this.val = this.a;
        else if (this.val!=this.a && this.val!=this.b) {
            this.val -= this.step;
            this.val += step;
        }
        this.step = step;

        return this;
    }

}
公共类递增器实现Iterable{
int val,步骤a,b;
专用递增器(整数a、整数b、整数步长){
这个步骤=步骤;
这个a=a;
这个.b=b;
如果(步骤>0)
val=a;
其他的
val=b;
}
@凌驾
公共迭代器迭代器(){
返回新的迭代器(){
@凌驾
公共布尔hasNext(){
如果(步骤<0&&val>=a)
返回true;

否则,如果(步骤>0&&val您一开始就犯了一个基本错误:
Iterable
应该能够发出无限多个
迭代器,但您只能发出一个

每个
迭代器
都应该有足够的内部状态,以便能够在您的值集上进行迭代

要同时解决此问题和其他问题,请在
iterator()中将代码更改为:

并删除另一个
val

(另外,我建议您将
a
重命名为
start
,将
b
重命名为
end


最后一句话:为了完全遵守迭代器的合同,您的
.remove()
应该:

public void remove()
{
    throw new UnsupportedOperationException();
}

不,您不需要声明该方法抛出此异常,因为它是未检查的异常。请参阅javadoc for
RuntimeException
,您首先犯了一个基本错误:
Iterable
应该能够发出无限多个
迭代器,但您只能发出一个

每个
迭代器
都应该有足够的内部状态,以便能够在您的值集上进行迭代

要同时解决此问题和其他问题,请在
iterator()中将代码更改为:

并删除另一个
val

(另外,我建议您将
a
重命名为
start
,将
b
重命名为
end


最后一句话:为了完全遵守迭代器的合同,您的
.remove()
应该:

public void remove()
{
    throw new UnsupportedOperationException();
}

不,您不需要声明该方法引发此异常,因为它是未检查的异常。请参阅javadoc for
RuntimeException

谢谢我编辑了OP。请看一看。再次感谢。谢谢,我像您所说的那样再次编辑了代码。但不幸的是,输出与原始帖子中发布的相同。谢谢请看一看。哦,这是一次行动edited@Yoda编辑了我的答案。谢谢。我已经编辑了OP。请看一看。再次感谢。谢谢。我再次编辑了代码,正如你所说。但不幸的是,输出与原始帖子中发布的相同。请看一看。噢,OP是edited@Yoda编辑了我的答案。我应该如何设置
开始
end
步骤
到迭代器。我不知道如何通过方法
by()
访问它。(1,10)中的示例
。by(-1)
开始
应该设置为1,
结束
应该设置为10,
步骤
tp-1.Hmmm…这是你练习的要求吗?你不应该在迭代器运行时修改它的行为…是的,这是不幸的。在练习中,老师写道:://W trakcie iteracji można zmieniac krok,这是我写的ans迭代时您可以更改步骤。但这是一位非常糟糕的老师(不是我的老师)。哦,天哪……一位故意要求违约的老师:/我应该如何将
开始
结束
步骤
设置为迭代器。我不知道如何通过方法
通过()
访问它。(1,10)中的示例
开始
应该设置为1,
结束
应该设置为10,
步骤
tp-1.Hmmm…这是你练习的要求吗?你不应该在迭代器运行时修改它的行为…是的,这是不幸的
public void remove()
{
    throw new UnsupportedOperationException();
}