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.

Moreover, the java.util.ArrayList class has features <strong>in</strong> addition to those<br />

of our simplified array list ADT. For example, the class<br />

java.util.ArrayList also <strong>in</strong>cludes a method, clear(), which removes<br />

all the elements from the array list, <strong>and</strong> a method, toArray(), which returns an<br />

array conta<strong>in</strong><strong>in</strong>g all the elements of the array list <strong>in</strong> the same order. In addition,<br />

the class java.util.ArrayList also has methods for search<strong>in</strong>g the list,<br />

<strong>in</strong>clud<strong>in</strong>g a method <strong>in</strong>dexOf(e), which returns the <strong>in</strong>dex of the first occurrence<br />

of an element equal to e <strong>in</strong> the array list, <strong>and</strong> a method lastIndexOf(e),<br />

which returns the <strong>in</strong>dex of the last occurrence of an element equal to e <strong>in</strong> the array<br />

list. Both of these methods return the (<strong>in</strong>valid) <strong>in</strong>dex value − 1 if an element equal<br />

to e is not found.<br />

6.1.5 Implement<strong>in</strong>g an Array List Us<strong>in</strong>g Extendable<br />

Arrays<br />

In addition to implement<strong>in</strong>g the methods of the IndexList <strong>in</strong>terface (<strong>and</strong> some<br />

other useful methods), the class java.util.ArrayList provides an an<br />

<strong>in</strong>terest<strong>in</strong>g feature that overcomes a weakness <strong>in</strong> the simple array implementation.<br />

Specifically, a major weakness of the simple array implementation for the array list<br />

ADT given <strong>in</strong> Section 6.1.3, is that it requires advance specification of a fixed<br />

capacity, N, for the total number of elements that may be stored <strong>in</strong> the array list. If<br />

the actual number of elements, n, of the array list is much smaller than N, then this<br />

implementation will waste space. Worse, if n <strong>in</strong>creases past N, then this<br />

implementation will crash.<br />

Instead, the java.util.ArrayList uses an <strong>in</strong>terest<strong>in</strong>g extendable-array<br />

technique so that we never have to worry about array overflows when us<strong>in</strong>g this<br />

class.<br />

As with the java.util.ArrayList class, let us provide a means to grow the<br />

array A that stores the elements of an array list S. Of course, <strong>in</strong> <strong>Java</strong> (<strong>and</strong> other<br />

programm<strong>in</strong>g languages), we cannot actually grow the array A; its capacity is fixed<br />

at some number N, as we have already observed. Instead, when an overflow occurs,<br />

that is, when n = N <strong>and</strong> we make a call to the method add, we perform the<br />

follow<strong>in</strong>g additional steps:<br />

1. Allocate a new array B of capacity 2N<br />

2. Let B[i ]← A[i], for i = 0,... , N − 1<br />

3. Let A ← B, that is, we use B as the array support<strong>in</strong>g S<br />

4. Insert the new element <strong>in</strong> A.<br />

321

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

Saved successfully!

Ooh no, something went wrong!