First a recommendation:
If you want SortedList
to implement the List
interface, it's a good idea to extend AbstractList
instead of implementing List
directly. AbstractList
has already defined many of the necessary methods, including the one you're having problems with. Most List
-implementations in the Java platform libraries also extend AbstractList
.
If you still want to implement List
directly, here is what the method is supposed to do:
Let a
be the specified array.
- If
a
is large enough, fill it with the elements from yourSortedList
(in the correct order) without caring about what was previously ina
. - If there's room to spare in
a
after filling it, seta[size()] = null
. Then the user will know where the list ends, unless the list containsnull
-elements. - If the list doesn't fit in
a
, create a new array of typeT
with the same size as the list, and fill the new one instead. - Return the array you filled. If you filled
a
, returna
. If you made a new array, return the new array.
There are two reasons why this method is useful:
- The array will not necessarily be of type
Object
, but of a typeT
decided by the user (as long as the type is valid). - The user may want to save memory and re-use an array instead of allocating more mamory to make a new one.
Here is how the Java Docs describe the method.