迭代器返回错误的整数值 [英] Iterator returns wrong integer values

查看:103
本文介绍了迭代器返回错误的整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现类 Incrementer ,并且它假设实现 Iterable

I have to implement class Incrementer and it suppose to implement 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 

我的增量器类看起来像这样:

    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;
    }

}

和测试代码:

package in;
import static in.Incrementer.*;

public class Test {

  public static void main(String[] args) {

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


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

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

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

    Incrementer inc;
    for (int i : inc = in(1,10) ) {
      if (i == 4) inc.by(2);
      System.out.print(i + " ");
    }
    System.out.println();
    for (int i : inc = in(1,10) ) {
      if (i == 8) inc.by(-2);
      System.out.print(i + " ");
    }
    System.out.println();
    for(int k : inc = in(10, 1)) {
      if (k == 5) inc.by(1);
      System.out.print(k + " ");
    }

  }


}

我不知道我错在哪里。

推荐答案

错误是你没有初始化 val ,因此它将从 0 开始(默认值)。

The mistake is that you doesn't initialize val, so it will start at 0 (the default value).

在你的第二个例子中,你将返回val + = step; val = 0 step = 2 ,所以它将从 2 开始并从那里继续。

In your second example, you will return val += step;, with val = 0 and step = 2, so it will start at 2 and continue from there.

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

In your third example, a = 10, b = 1, step = -1 and val = 0, so you will not enter in

if (step < 0 && val > a)

因为 val< a ,你不会输入

else if (step > 0 && val < b)

因为 step< 0

编辑:

在编辑后,您应该修改 next()方法以返回 val ,并且只在以下情况后增加它:

In the edited post, you should modify the next() method to return val, and only increase it after :

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

您还应该修改 hasNext中的条件( )

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

要进行第四次测试工作,您必须更改 by()如果需要,反转 a b 的方法:

To make you fourth test work, you will have to change the by() method to invert a and b if needed:

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;
}

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

You can also test the inverse case:

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






以下是完整代码:


Here is the complete code:

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;
    }

}

这篇关于迭代器返回错误的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆