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.

<strong>and</strong> then fill the array back up aga<strong>in</strong> by popp<strong>in</strong>g the elements off of the stack. In<br />

Code Fragment 5.8, we give a <strong>Java</strong> implementation of this algorithm. Incidentally,<br />

this method also illustrates how we can use generic types <strong>in</strong> a simple application<br />

that uses a generic stack. In particular, when the elements are popped off the stack<br />

<strong>in</strong> this example, they are automatically returned as elements of the E type; hence,<br />

they can be immediately returned to the <strong>in</strong>put array. We show an example use of<br />

this method <strong>in</strong> Code Fragment 5.9.<br />

Code Fragment 5.8: A generic method that<br />

reverses the elements <strong>in</strong> an array of type E objects,<br />

us<strong>in</strong>g a stack declared us<strong>in</strong>g the Stack <strong>in</strong>terface.<br />

Code Fragment 5.9: A test of the reverse<br />

method us<strong>in</strong>g two arrays.<br />

278

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

Saved successfully!

Ooh no, something went wrong!