Having trouble with iterator. It need to iterate over ArrayList and find any other Object than this and print it’s name. Program uses Threads. How can iterator produce error when it entered while loop when itr.hasNext() was equal true?
Stack trace:
Thread [JOE] (Suspended (exception NoSuchElementException))
ArrayList$Itr.next() line: not available [local variables unavailable]
Monster.beginFight(Monster) line: 55
Monster.run() line: 28
Method
private void tryName(Monster monster){
Iterator<Monster> itr = room.monsters.iterator();
while(itr.hasNext()){
if (!itr.next().equals(monster)) {
System.out.println(monster.getName() + "some text "
+ itr.next().getName());
break;
}
}
}
Why do I have in ArrayList$Itr.next() tab displaying: Source not found?
Every time you call
itr.nextit will advance the iterator to the next element. You need to do something like: