开发者

how can i get two consecutive values from Iterator

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.


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.


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


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();?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜