Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Iterator pattern remove method

Ranch Hand
Posts: 153
1
• Number of slices to send:
Optional 'thank-you' note:
A snippet from Head First Design Patterns:

I don't understand what the remove() method is doing in relation to what was stated: how are we moving everything else down by 1 if list[i+1] seems to be moving everything else up by 1?

What exactly is happening in the remove() method? The current explanation doesn't inform me well enough.

author
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:

Ryan McClain wrote:
I don't understand what the remove() method is doing in relation to what was stated: how are we moving everything else down by 1 if list[i+1] seems to be moving everything else up by 1?

I recommend using a pencil and paper, draw out an array, picking one of the elements as index i, and follow the for loop in the example (basically, pretend you are the JVM). You will see that the algorithm is indeed moving everything after index i down by 1.

Henry

Ryan McClain
Ranch Hand
Posts: 153
1
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:

Ryan McClain wrote:
I don't understand what the remove() method is doing in relation to what was stated: how are we moving everything else down by 1 if list[i+1] seems to be moving everything else up by 1?

I recommend using a pencil and paper, draw out an array, picking one of the elements as index i, and follow the for loop in the example (basically, pretend you are the JVM). You will see that the algorithm is indeed moving everything after index i down by 1.

Henry

I did that before I posted this question and it doesn't help me.. here's what I wrote down:

I must be getting something wrong.

Henry Wong
author
Posts: 23956
142
• 1
• Number of slices to send:
Optional 'thank-you' note:

Ryan McClain wrote:
I did that before I posted this question and it doesn't help me.. here's what I wrote down:

I must be getting something wrong.

Well, if i is at index zero, then doesn't "list[i] = list[i+1]" copy the element at index 1 down to index zero? then with the next iteration of the for loop, if i is now at index one, then doesn't "list[i] = list[i+1]" copy the element at index 2 down to index 1? and of course, there is a line after the for loop -- can you figure out what it does?

Henry

Sheriff
Posts: 17652
300
• Number of slices to send:
Optional 'thank-you' note:
Just to make sure everyone is using the same definition, moving "down" means to move a value from a higher index position in the array to a lower index position. Moving up would put a value further away from element 0 of the array.

Ryan McClain
Ranch Hand
Posts: 153
1
• Number of slices to send:
Optional 'thank-you' note:
Ohh.. I was reading it wrongly: from LTR instead of RTL. I also forgot about the byref/byval principle, which made me confused here.
Yes, the last line nullifies the last element in the array.

Thanks

 But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser