我如何从迭代器中获取两个连续的值
这是我尝试获取迭代器的两个连续元素的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最终的问题在于
while
语句。您的代码将不仅仅从迭代器中获取前两个元素。相反,如果迭代器中有偶数个元素,它将获取最后两个。如果元素数量为奇数,那么您将获得相同的 x 和 y 值。具体来说,是最后一个元素。更根本的是,您的代码的问题是
u
、v
、x
和y
在您的方法之外声明。我假设您这样做是因为您不知道如何返回多个值。如果需要返回多个值,请返回元素数组,或返回自定义容器类。下面是如何在数组中返回从给定迭代器中取出的两个元素的示例:
请注意,如果迭代器中只剩下一个值,则返回数组的第二个元素将为
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
andy
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:
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 benull
if the Iterator is empty.看起来不错。你的问题出在别的地方。也许两者的
index1
是一样的?或者迭代器中只有一项?以下示例
按预期打印以下内容
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
prints as expected the following
为什么不在
else
周围使用大括号,并避免不必要的变量赋值:此代码只会为
x
和y
提供相同的值如果两个连续元素具有相同的index1
,或者没有下一个值(元素数量为奇数)。另一方面,您确定您的第一行不应该是
Iterator; tr = value.iterator();
?Why don't you use braces around your
else
, and avoid needless variable assignments:This code will only give the same value for
x
andy
if either two consecutive elements have the sameindex1
, 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();
?您更新的代码不会改变任何内容。 @BalusC 的诊断仍然是正确的。问题不在于这部分代码。
可能的原因包括:
Value
元素或奇数个Value
元素Value
元素的值相同列表中的index1
Value
元素插入到列表中。 (也许您忘记使用 usenew 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:
Value
element or an odd number ofValue
elements in the iteratorValue
elements with the same value for theirindex1
in the listValue
element into the list multiple times. (Maybe you forgot to use usenew 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.