【问题标题】:method next() of an iterator迭代器的方法 next()
【发布时间】:2014-01-09 14:01:05
【问题描述】:

当我在 ArrayList 上获得迭代器并在其上使用 next() 方法时, 它是只返回下一个元素还是返回并从迭代器中删除元素。 喜欢

Iterator i = list.iterator();
while(i.hasNext())
    System.out.print(i.next()+" ");

【问题讨论】:

  • 别担心,remove() 删除。
  • 也许在迭代之后?不是在迭代期间。你能回答这个“小”问题吗?
  • 是的,迭代后i.hasNext()返回false
  • 在增强的 for 循环中使用 ArrayList,请参阅此处stackoverflow.com/questions/11685305/…

标签: java iterator


【解决方案1】:

调用 next() 只是将指针移动到下一个元素
并返回元素。它不会删除任何元素。

有关详细信息,请参见此处。

http://en.wikipedia.org/wiki/Iterator#Java

http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

迭代器也有一个 remove() 方法。该方法会删除元素。

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多