Java Reference
In-Depth Information
Consider the following implementation of the clear method (which
empties any collection).
6.15
public abstract class AbstractCollection<AnyType>
implements Collection<AnyType>
{
public void clear( )
{
Iterator<AnyType> itr = this.iterator( );
while( itr.hasNext( ) )
{
itr.next( );
itr.remove( );
}
}
...
}
Suppose LinkedList extends AbstractCollection and does not
override clear . What is the running time of clear ?
a.
Suppose ArrayList extends AbstractCollection and does not over-
ride clear . What is the running time of clear ?
b.
Suppose it takes 4 seconds to run clear on a 100,000-item ArrayList .
How long will it take to run clear on a 500,000-item ArrayList ?
c.
As clearly as possible, describe the behavior of this alternate
implementation of clear :
d.
public void clear( )
{
for( AnyType item : this )
this.remove( item );
}
Static method removeHalf removes the first half of a List (if there are
an odd number of items, then slightly less than one-half of the list is
removed). One possible implementation of removeHalf is shown
below:
6.16
public static void removeHalf( List<?> lst )
{
int size = lst.size( );
for( int i = 0; i < size / 2; i++ )
lst.remove( 0 );
}
a.
Why can't we use lst.size( )/2 as the test in the for loop?
Search WWH ::




Custom Search