Java Reference
In-Depth Information
41 /**
42 * Returns true if this collection contains x.
43 * If x is null, returns false.
44 * (This behavior may not always be appropriate.)
45 * @param x the item to search for.
46 * @return true if x is not null and is found in
47 * this collection.
48 */
49 public boolean contains( Object x )
50 {
51 if( x == null )
52 return false;
53
54 for( AnyType val : this )
55 if( x.equals( val ) )
56 return true;
57
58 return false;
59 }
60
61 /**
62 * Removes non-null x from this collection.
63 * (This behavior may not always be appropriate.)
64 * @param x the item to remove.
65 * @return true if remove succeeds.
66 */
67 public boolean remove( Object x )
68 {
69 if( x == null )
70 return false;
71
72 Iterator<AnyType> itr = iterator( );
73 while( itr.hasNext( ) )
74 if( x.equals( itr.next( ) ) )
75 {
76 itr.remove( );
77 return true;
78 }
79
80 return false;
81 }
figure 15.11
Sample
implementation of
AbstractCollection
(part 2)
Figure 15.12 contains the implementations of the two toArray methods.
The zero-parameter toArray is fairly simple to implement. The one-parameter
toArray makes use of a feature of Java known as reflection to create an array
object that matches the parameter type in the case that the parameter is not
large enough to store the underlying collection.
 
Search WWH ::




Custom Search