Java 迭代器 next 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9707085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterator next method
提问by Delta
In ResultSet
interface there is next
method which return boolean and then you can directly access the current record by get
methods
在ResultSet
接口中有next
返回布尔值的方法,然后您可以通过get
方法直接访问当前记录
Why Iterator
from java.util
just not drop hasNext()
method and have only next
method which will move the cursor to next element and return boolean
?
为什么Iterator
从java.util
刚才不降hasNext()
法,只next
将光标移动到下一个元素,并返回方法boolean
?
回答by Jon Skeet
Because next()
currently returns the next item.
因为next()
当前返回下一个项目。
Java couldhave implemented the iterator pattern in the same way that .NET does, with a MoveNext()
returning a Boolean value, and then a Current
property with the "current" value - but it's still two members...
Java可以以与 .NET 相同的方式实现迭代器模式,MoveNext()
返回一个布尔值,然后是一个Current
具有“当前”值的属性 - 但它仍然是两个成员......
The other alternative is having one return value which encapsulates the two ideas of "is there a value" and "what is the value if there is one" - a sort of Maybe
type, really. Of course in Java, that means allocating a new object on each iteration, which isn't ideal...
另一种选择是有一个返回值,它封装了“是否有值”和“如果有值是什么”这两个想法——一种Maybe
类型,真的。当然,在 Java 中,这意味着在每次迭代时分配一个新对象,这并不理想......
回答by AlexR
The next()
method of ResultSet
is like hasNext()
of iterator. The majority of other methods of ResultSet
are instead of single next()
method of Iterator.
的next()
方法ResultSet
类似于hasNext()
迭代器。大多数其他方法ResultSet
都代替next()
了 Iterator的单一方法。
I'd ask other question: why they did not make ResultSet
implement iterator or at least iterable with generics? I think the answer is that ResultSet
appears in java before Iterator. Shortly after that a lot of ORM-like tools were introduced, so most people just do not use JDBC directly and do not deal with ResultSet
.
我会问另一个问题:为什么他们没有ResultSet
实现迭代器或至少可以用泛型迭代?我认为答案是ResultSet
出现在 java 中的 Iterator 之前。之后不久就引入了很多类似ORM的工具,所以大多数人只是不直接使用JDBC,也不处理ResultSet
.
But I am not historian of JDK, so it is my assumption.
但我不是 JDK 的历史学家,所以这是我的假设。
回答by Jay
Because you would still need another function to retrieve the value, so nothing would be gained.
因为您仍然需要另一个函数来检索该值,所以什么也得不到。
In a ResultSet, next() gets you to the next record, or returns false if there is none. Then you do getString()'s, getInt()'s or whatever to retrieve field values. So you write code like:
在 ResultSet 中,next() 将您带到下一条记录,如果没有,则返回 false。然后您执行 getString()'s、getInt()'s 或其他任何检索字段值的操作。所以你写的代码如下:
while (rs.next())
{
name=rs.getString("name"); // or whatever
... do something with name ...
}
In an Iterator, hasNext() returns a true or false to indicate if there's another record. Then you call next() to retrieve the value. so you write code like:
在 Iterator 中,hasNext() 返回 true 或 false 以指示是否有另一条记录。然后调用 next() 来检索值。所以你写的代码如下:
while (iter.hasnext())
{
name=iter.next(); // or whatever
... do something with name ...
}
The pattern ends up being pretty similar. It's unfortunate that the implementations are inconsistent. That is, ResultSet.next not only tells you if you're at the end but also advances the positoin, while Iterator.hasNext tells you if you're at the end but doesn't advance the position. But actual use is pretty similar.
这种模式最终非常相似。不幸的是,实现不一致。也就是说, ResultSet.next 不仅会告诉您是否在最后,而且还会提高位置,而 Iterator.hasNext 会告诉您是否在最后但不会提高位置。但实际使用非常相似。
It wouldn't really work to try to combine Iterator.hasNext and Iterator.next into one function. How would you know when you reached the end? You could say that it returns null at the end ... but what if null is a valid value in the list? I suppose you could add some magic value, but you'd still have the problem of distinguishing the magic meaning from an entry in the list that just happened to have that value. Like if you have a list of ints, you could say that -1 means end-of-list, but then what if the list includes a value of -1?
尝试将 Iterator.hasNext 和 Iterator.next 合并为一个函数是行不通的。你怎么知道什么时候到达终点?您可以说它最后返回 null ……但是如果 null 是列表中的有效值呢?我想你可以添加一些魔法值,但你仍然会遇到将魔法含义与列表中恰好具有该值的条目区分开来的问题。就像如果你有一个整数列表,你可以说 -1 表示列表结束,但是如果列表包含 -1 的值呢?
The only other alternative I see is to have the next() function return an object that includes both an end-of-list indication and, if applicable, the value. But this would be a pain to use. You'd have to write something like:
我看到的唯一另一种选择是让 next() 函数返回一个对象,该对象包括列表结束指示和值(如果适用)。但这将是一个痛苦的使用。你必须写这样的东西:
while (true)
{
IterableResult ir=iter.next();
if (ir.end)
{
break;
}
else
{
name=ir.value;
... do something with name ...
}
}
That doesn't seem to gain anything.
这似乎没有任何收获。
Maybe there's another approach that would work better, but I can't think of one. And apparently the creators of the Iterator interface couldn't think of a better way either! :-)
也许还有另一种更好的方法,但我想不出一个。显然,Iterator 接口的创建者也想不出更好的方法!:-)