java.util.NoSuchElementException using iterator in java

后端 未结 1 351
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 15:13

I\'m trying to iterate through a list using the iterator over my list of Logs. The goal is to search for a logs which contains the same phonenumber, type and date as the new

相关标签:
1条回答
  • 2020-12-16 15:41

    You are calling next() a bunch of times in one iteration forcing the Iterator to move to an element that doesn't exist.

    Instead of

    if (iterator.next().getPhonenumber() == phonenumber  && iterator.next().getType() == type && iterator.next().getDate() == date)
    {
        updateLog(newLog, iterator.next().getId());
        ...
    

    Use

    Log log = iterator.next();
    
    if (log.getPhonenumber() == phonenumber  && log.getType() == type && log.getDate() == date)
    {
        updateLog(newLog, log .getId());
        ...
    

    Every time you call Iterator#next(), it moves the underlying cursor forward.

    0 讨论(0)
提交回复
热议问题