23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Some Subtle Po<strong>in</strong>ts About Entry Removal<br />

The details for do<strong>in</strong>g the remove operation conta<strong>in</strong> a few subtle po<strong>in</strong>ts. The first is<br />

that, <strong>in</strong> order to remove <strong>and</strong> return the game entry (let's call it e) at <strong>in</strong>dex i <strong>in</strong> our<br />

array, we must first save e <strong>in</strong> a temporary variable. We will use this variable to<br />

return e when we are done remov<strong>in</strong>g it. The second subtle po<strong>in</strong>t is that, <strong>in</strong> mov<strong>in</strong>g<br />

references higher than i one cell to the left, we don't go all the way to the end of<br />

the array—we stop at the second to last reference. We stop just before the end,<br />

because the last reference does not have any reference to its right (hence, there is<br />

no reference to move <strong>in</strong>to the last place <strong>in</strong> the entries array). For the last<br />

reference <strong>in</strong> the entries array, it is enough that we simply null it out. We<br />

conclude by return<strong>in</strong>g a reference to the removed entry (which no longer has any<br />

reference po<strong>in</strong>t<strong>in</strong>g to it <strong>in</strong> the entries array). See Code Fragment 3.4.<br />

Code Fragment 3.4: <strong>Java</strong> code for perform<strong>in</strong>g the<br />

remove operation.<br />

148

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!