views:

89

answers:

4

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.

A: 

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

Borealid
Yes, you are right, But my method is like thispublic 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.
tsegay
@tsegay: Don't pass the Iterator to the function. Pass the object instead.
Borealid
+1  A: 

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
BalusC
Thanks for the answer and example, but where could be the problem. can you check again my code, i have updated it.
tsegay
This doesn't change things. I've already mentioned two possible causes of your problem.
BalusC
A: 

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.

Stephen C
+1  A: 

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.

Gunslinger47
Thanks, Clean and helpful.
tsegay
@tsegay: Please remember to "accept" answers to your question, even if it's your own answer. This lets people know the question has been answered, and gives them a reward for helping you.
Gunslinger47
Thanks, for reminding me. Of course you deserve that Sir.
tsegay