Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic