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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This array replacement strategy is known as an extendable array, for it can be<br />

viewed as extend<strong>in</strong>g the end of the underly<strong>in</strong>g array to make room for more<br />

elements. (See Figure 6.2.) Intuitively, this strategy is much like that of the hermit<br />

crab, which moves <strong>in</strong>to a larger shell when it outgrows its previous one.<br />

Figure 6.2: An illustration of the three steps for<br />

"grow<strong>in</strong>g" an extendable array: (a) create new array B;<br />

(b) copy elements from A to B; (c) reassign reference A<br />

to the new array. Not shown is the future garbage<br />

collection of the old array.<br />

Implement<strong>in</strong>g the IndexList Interface with an<br />

Extendable Array<br />

We give portions of a <strong>Java</strong> implementation of the array list ADT us<strong>in</strong>g an<br />

extendable array <strong>in</strong> Code Fragment 6.3. This class only provides means for the<br />

array to grow. Exercise C-6.2 explores an implementation that can also shr<strong>in</strong>k.<br />

Code Fragment 6.3: Portions of class<br />

ArrayIndexList realiz<strong>in</strong>g the array list ADT by<br />

means of an extendable array. Method<br />

checkIndex(r, n) (not shown) checks whether an<br />

<strong>in</strong>dex r is <strong>in</strong> the range [0, n − 1].<br />

322

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

Saved successfully!

Ooh no, something went wrong!