Java Reference
In-Depth Information
1
package weiss.util;
2
3
public class ArrayList<AnyType> extends AbstractCollection<AnyType>
4
implements List<AnyType>
5
{
6
private static final int DEFAULT_CAPACITY = 10;
7
private static final int NOT_FOUND = -1;
8
9
private AnyType [ ] theItems;
10
private int theSize;
11
private int modCount = 0;
12
13
public ArrayList( )
14
{ clear( ); }
15
16
public ArrayList( Collection<? extends AnyType> other )
17
{
18
clear( );
19
for( AnyType obj : other )
20
add( obj );
21
}
22
23
public int size( )
24
{ return theSize; }
25
26
public void clear( )
27
{
28
theSize = 0;
29
theItems = (AnyType []) new Object[ DEFAULT_CAPACITY ];
30
modCount++;
31
}
32
33
public AnyType get( int idx )
34
{
35
if( idx < 0 || idx >= size( ) )
36
throw new ArrayIndexOutOfBoundsException( );
37
return theItems[ idx ];
38
}
39
40
public AnyType set( int idx, AnyType newVal )
41
{
42
if( idx < 0 || idx >= size( ) )
43
throw new ArrayIndexOutOfBoundsException( );
44
AnyType old = theItems[ idx ];
45
theItems[ idx ] = newVal;
46
47
return old;
48
}
49
50
public boolean contains( Object x )
51
{ return findPos( x ) != NOT_FOUND; }
figure 15.13
ArrayList
implementation
(part 1)
Search WWH ::
Custom Search