Java Reference
In-Depth Information
1 /**
2 * The StringArrayList implements a growable array of Strings.
3 * Insertions are always done at the end.
4 */
5 public class StringArrayList
6 {
7 /**
8 * Returns the number of items in this collection.
9 * @return the number of items in this collection.
10 */
11 public int size( )
12 {
13 return theSize;
14 }
15
16 /**
17 * Returns the item at position idx.
18 * @param idx the index to search in.
19 * @throws ArrayIndexOutOfBoundsException if index is bad.
20 */
21 public String get( int idx )
22 {
23 if( idx < 0 || idx >= size( ) )
24 throw new ArrayIndexOutOfBoundsException( );
25 return theItems[ idx ];
26 }
27
28 /**
29 * Adds an item to this collection at the end.
30 * @param x any object.
31 * @return true (as per java.util.ArrayList).
32 */
33 public boolean add( String x )
34 {
35 if( theItems.length == size( ) )
36 {
37 String [ ] old = theItems;
38 theItems = new String[ theItems.length * 2 + 1 ];
39 for( int i = 0; i < size( ); i++ )
40 theItems[ i ] = old[ i ];
41 }
42
43 theItems[ theSize++ ] = x;
44 return true;
45 }
46
47 private static final int INIT_CAPACITY = 10;
48
49 private int theSize = 0;
50 private String [ ] theItems = new String[ INIT_CAPACITY ];
51 }
figure 3.17
Simplified
StringArrayList with
add , get , and size
 
Search WWH ::




Custom Search