+ 1

Iterators in Java

See the output of my code. Why is the iterator pointing to the same memory? It doesn't change the memory while going to next element? https://code.sololearn.com/cA2IAQZ9SGOW/?ref=app

26th Aug 2021, 2:59 PM
Rishi
Rishi - avatar
4 Respostas
+ 4
'it' is a reference variable which is pointing to your list. It gets a memory allocated in the heap which will not change until you reference it to some other object. It is the next() method which is iterating over the list and not the 'it' itself.
26th Aug 2021, 4:10 PM
Avinesh
Avinesh - avatar
+ 2
iterator is object with variable where is stored position of actual element. What is changed in memory is value of this variable, not address of object iterator itself.
26th Aug 2021, 4:49 PM
zemiak
+ 1
You have assigned iterator to it once, and didnt reassinged. You expect it to take two memory locations? See reassigning, it=arr.iterator again creates another iterator on different memory. Thats what you are expecting
26th Aug 2021, 3:18 PM
Harshit Jawla
Harshit Jawla - avatar
+ 1
Oh that's what happening, thank you all, I understand :)
27th Aug 2021, 4:25 AM
Rishi
Rishi - avatar