+ 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
4 Answers
+ 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.
+ 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.
+ 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
+ 1
Oh that's what happening, thank you all, I understand :)