【问题标题】:ListIterators .next() will advance to next even inside the if condition? [closed]ListIterators .next() 即使在 if 条件内也会前进到下一个? [关闭]
【发布时间】:2020-07-29 09:18:31
【问题描述】:

在 Java 列表迭代器中,使用 iterator.next() 将其带到下一个值,即使它在 if 语句中使用?

if (iterator.next() == "A") {...}

【问题讨论】:

  • 是的,阅读更多关于迭代器的信息。 geeksforgeeks.org/how-to-use-iterator-in-java
  • 方法的作用不依赖于使用的上下文;)
  • 您是在问iterator.next() 是否总是使指针前进?如果是这样,“是”就是答案。

标签: java iterator


【解决方案1】:

是的,next() 方法总是前进到下一个值,即使在 if 条件中使用也是如此。下面是方法说明,

 /**
 * Returns the next element in the iteration.
 *
 * @return the next element in the iteration
 * @throws NoSuchElementException if the iteration has no more elements
 */
 E next();

【讨论】:

    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多