Reputation: 1028
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.
Upvotes: 2
Views: 10103
Reputation: 7061
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.
Upvotes: 3
Reputation: 718758
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:
Value
element or an odd number of Value
elements in the iteratorValue
elements with the same value for their index1
in the listValue
element into the list multiple times. (Maybe you forgot to use use new Value(...)
to create each element?)Iterator
class that is behaving incorrectly.But without seeing the relevant code, we can only guess as to what the real problem might be.
Upvotes: 0
Reputation: 1108662
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
Upvotes: 2
Reputation: 98469
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();
?
Upvotes: 0