我如何从迭代器中获取两个连续的值

发布于 2024-09-13 07:58:25 字数 522 浏览 3 评论 0原文

这是我尝试获取迭代器的两个连续元素的代码。

public void Test(Iterator<Value> values) {
    Iterator<Value> tr = values;
    while (tr.hasNext()) {
        v = tr.next();
        x = v.index1;
        // u = null;

        if (tr.hasNext()) {
            u = tr.next();
            y = u.index1;
        } else {
            u = v;
            y = u.index1;
        }

        System.out.println(x);
        System.out.println(y);
    }
}

但我仍然得到相同的 x 和 Y 值。

这有什么问题,我得到两个变量 x 和 y 的相同值。

Here is my code that i tried to get two consecutive elements of Iterator.

public void Test(Iterator<Value> values) {
    Iterator<Value> tr = values;
    while (tr.hasNext()) {
        v = tr.next();
        x = v.index1;
        // u = null;

        if (tr.hasNext()) {
            u = tr.next();
            y = u.index1;
        } else {
            u = v;
            y = u.index1;
        }

        System.out.println(x);
        System.out.println(y);
    }
}

But still i am getting same values for x and Y.

What is wrong with this, i am getting the same value for the two variables x and y.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南汐寒笙箫 2024-09-20 07:58:25

最终的问题在于 while 语句。您的代码将不仅仅从迭代器中获取前两个元素。相反,如果迭代器中有偶数个元素,它将获取最后两个。如果元素数量为奇数,那么您将获得相同的 x 和 y 值。具体来说,是最后一个元素。

更根本的是,您的代码的问题是 uvxy 在您的方法之外声明。我假设您这样做是因为您不知道如何返回多个值。如果需要返回多个值,请返回元素数组,或返回自定义容器类。

下面是如何在数组中返回从给定迭代器中取出的两个元素的示例:

public static Value[] nextTwo(Iterator<Value> values) {
    return new Value[] {
        (values.hasNext()?values.next():null),  
        (values.hasNext()?values.next():null)
    };
}

请注意,如果迭代器中只剩下一个值,则返回数组的第二个元素将为 null 。如果迭代器为空,则数组的两个元素都将为 null

The ultimate problem is with the while statement. Your code will not just grab the first two elements from the Iterator. Rather, if there is an even number of elements in the Iterator, it'll grab the last two. If there's an odd number of elements then you'll get the same value for x and y. Specifically, the last element.

More fundamentally, the problem with your code is u, v, x and y are declared outside of your method. I assume you're doing this because you don't know how to return more than one value. If you need to return multiple values, return an array of elements, or return a custom container class.

Here's an example of how you can return in an array the two elements taken off of a given Iterator:

public static Value[] nextTwo(Iterator<Value> values) {
    return new Value[] {
        (values.hasNext()?values.next():null),  
        (values.hasNext()?values.next():null)
    };
}

Note that the second element of the returned array will be null if the Iterator only has one value left in it. Both elements of the array will be null if the Iterator is empty.

半透明的墙 2024-09-20 07:58:25

看起来不错。你的问题出在别的地方。也许两者的 index1 是一样的?或者迭代器中只有一项?

以下示例

List<String> strings = Arrays.asList("one", "two", "three", "four", "five");
Iterator<String> iter = strings.iterator();
while (iter.hasNext()) {
    String first = iter.next();
    String second = iter.hasNext() ? iter.next() : first;

    System.out.printf("First: %s, Second: %s%n", first, second);
}

按预期打印以下内容

First: one, Second: two
First: three, Second: four
First: five, Second: five

It looks fine. Your problem lies somewhere else. Probably index1 of both are just the same? Or you have only one item in the iterator?

The following example

List<String> strings = Arrays.asList("one", "two", "three", "four", "five");
Iterator<String> iter = strings.iterator();
while (iter.hasNext()) {
    String first = iter.next();
    String second = iter.hasNext() ? iter.next() : first;

    System.out.printf("First: %s, Second: %s%n", first, second);
}

prints as expected the following

First: one, Second: two
First: three, Second: four
First: five, Second: five
浪漫之都 2024-09-20 07:58:25

为什么不在 else 周围使用大括号,并避免不必要的变量赋值:

    while (tr.hasNext()) {
        v = tr.next();
        x = v.index1;
        // u = null;
        if (tr.hasNext()) {
            u = tr.next();
            y = u.index1;
        } else {
            y = v.index1;
        }
        System.out.println(x);
        System.out.println(y);
    }

此代码只会为 xy 提供相同的值如果两个连续元素具有相同的 index1,或者没有下一个值(元素数量为奇数)。

另一方面,您确定您的第一行不应该是 Iterator; tr = value.iterator();

Why don't you use braces around your else, and avoid needless variable assignments:

    while (tr.hasNext()) {
        v = tr.next();
        x = v.index1;
        // u = null;
        if (tr.hasNext()) {
            u = tr.next();
            y = u.index1;
        } else {
            y = v.index1;
        }
        System.out.println(x);
        System.out.println(y);
    }

This code will only give the same value for x and y if either two consecutive elements have the same index1, or there is no next value (odd number of elements).

On another note, are you sure your first line shouldn't be Iterator<Value> tr = values.iterator();?

空心↖ 2024-09-20 07:58:25

您更新的代码不会改变任何内容。 @BalusC 的诊断仍然是正确的。问题不在于这部分代码。

可能的原因包括:

  • 迭代器中有一个 Value 元素或奇数个 Value 元素
  • 您的 Value 元素的值相同列表中的 index1
  • 您已多次将相同的 Value 元素插入到列表中。 (也许您忘记使用 use new Value(...) 来创建每个元素?)
  • 您使用的是行为不正确的自定义 Iterator 类。

但在没有看到相关代码的情况下,我们只能猜测真正的问题可能是什么。

Your updated code doesn't change anything. @BalusC's diagnosis is still correct. The problem is not in this part of the code.

Possible causes include:

  • You have one Value element or an odd number of Value elements in the iterator
  • You have Value elements with the same value for their index1 in the list
  • You have inserted the same Value element into the list multiple times. (Maybe you forgot to use use new Value(...) to create each element?)
  • You are using a custom Iterator class that is behaving incorrectly.

But without seeing the relevant code, we can only guess as to what the real problem might be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文