Java Reference
In-Depth Information
82
/**
83
* Obtains a primitive array view of the collection.
84
* @return the primitive array view.
85
*/
86
public Object [ ] toArray( )
87
{
88
Object [ ] copy = new Object[ size( ) ];
89
int i = 0;
90
91
for( AnyType val : this )
92
copy[ i++ ] = val;
93
94
return copy;
95
}
96
97
public <OtherType> OtherType [ ] toArray( OtherType [ ] arr )
98
{
99
int theSize = size( );
100
101
if( arr.length < theSize )
102
arr = ( OtherType [ ] ) java.lang.reflect.Array.newInstance(
103
arr.getClass( ).getComponentType( ), theSize );
104
else if( theSize < arr.length )
arr[ theSize ] = null;
105
106
107
Object [ ] copy = arr;
int i = 0;
108
109
110
for( AnyType val : this )
111
copy[ i++ ] = val;
112
113
return copy;
114
}
115
116
/**
117
* Return a string representation of this collection.
118
*/
119
public String toString( )
120
{
121
StringBuilder result = new StringBuilder( "[ " );
122
123
for( AnyType obj : this )
124
result.append( obj + " " );
125
126
result.append( "]" );
127
128
return result.toString( );
129
}
130
}
figure 15.12
Sample implementation of
AbstractCollection
(part 3)
Search WWH ::
Custom Search