Java Reference
In-Depth Information
52
private int findPos( Object x )
53
{
54
for( int i = 0; i < size( ); i++ )
55
if( x == null )
56
{
57
if( theItems[ i ] == null )
58
return i;
59
}
60
else if( x.equals( theItems[ i ] ) )
61
return i;
62
63
return NOT_FOUND;
64
}
65
66
public boolean add( AnyType x )
67
{
68
if( theItems.length == size( ) )
69
{
70
AnyType [ ] old = theItems;
71
theItems = (AnyType []) new Object[ theItems.length * 2 + 1 ];
72
for( int i = 0; i < size( ); i++ )
73
theItems[ i ] = old[ i ];
74
}
75
theItems[ theSize++ ] = x;
76
modCount++;
77
return true;
78
}
79
80
public boolean remove( Object x )
81
{
82
int pos = findPos( x );
83
84
if( pos == NOT_FOUND )
85
return false;
86
else
87
{
88
remove( pos );
89
return true;
90
}
91
}
92
93
public AnyType remove( int idx )
94
{
95
AnyType removedItem = theItems[ idx ];
96
for( int i = idx; i < size( ) - 1; i++ )
97
theItems[ i ] = theItems[ i + 1 ];
98
theSize--;
99
modCount++;
100
return removedItem;
101
}
figure 15.14
ArrayList
implementation
(part 2)
Search WWH ::
Custom Search