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.

to that of the rank of an element <strong>in</strong> a list, which is usually def<strong>in</strong>ed to be one more<br />

than its <strong>in</strong>dex; so the first element is at rank 1, the second is at rank 2, <strong>and</strong> so on.<br />

A sequence that supports access to its elements by their <strong>in</strong>dices is called an array list<br />

(or vector, us<strong>in</strong>g an older term). S<strong>in</strong>ce our <strong>in</strong>dex def<strong>in</strong>ition is more consistent with the<br />

way arrays are <strong>in</strong>dexed <strong>in</strong> <strong>Java</strong> <strong>and</strong> other programm<strong>in</strong>g languages (such as C <strong>and</strong><br />

C++), we will be referr<strong>in</strong>g to the place where an element is stored <strong>in</strong> an array list as<br />

its "<strong>in</strong>dex," not its "rank" (although we may use r to denote this <strong>in</strong>dex, if the letter "i"<br />

is be<strong>in</strong>g used as a for-loop counter).<br />

This <strong>in</strong>dex concept is a simple yet powerful notion, s<strong>in</strong>ce it can be used to specify<br />

where to <strong>in</strong>sert a new element <strong>in</strong>to a list or where to remove an old element.<br />

6.1.1 The Array List Abstract <strong>Data</strong> Type<br />

As an ADT, an array list S has the follow<strong>in</strong>g methods (besides the st<strong>and</strong>ard<br />

size() <strong>and</strong> isEmpty() methods):<br />

get(i): Return the element of S with <strong>in</strong>dex i; an error condition<br />

occurs if i < 0 or i > size() − 1.<br />

set(i, e): Replace with e <strong>and</strong> return the element at <strong>in</strong>dex i; an error<br />

condition occurs if i < 0 or i > size() − 1.<br />

add(i, e): Insert a new element e <strong>in</strong>to S to have <strong>in</strong>dex i; an error<br />

condition occurs if i < 0 or i > size().<br />

remove(i): Remove from S the element at <strong>in</strong>dex i; an error condition<br />

occurs if i < 0 or i > size() − 1.<br />

We do not <strong>in</strong>sist that an array should be used to implement an array list, so that the<br />

element at <strong>in</strong>dex 0 is stored at <strong>in</strong>dex 0 <strong>in</strong> the array, although that is one (very<br />

natural) possibility. The <strong>in</strong>dex def<strong>in</strong>ition offers us a way to refer to the "place"<br />

where an element is stored <strong>in</strong> a sequence without hav<strong>in</strong>g to worry about the exact<br />

implementation of that sequence. The <strong>in</strong>dex of an element may change whenever<br />

the sequence is updated, however, as we illustrate <strong>in</strong> the follow<strong>in</strong>g example.<br />

Example 6.1: We show below some operations on an <strong>in</strong>itially empty array list<br />

S.<br />

Operation<br />

Output<br />

S<br />

add(0,7)<br />

314

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

Saved successfully!

Ooh no, something went wrong!