Java Reference
In-Depth Information
So you're left with two approaches for dealing with the
toArray
require-
ment: have the caller pass in the array and avoid warnings, but be
forced to deal with an array of the wrong size, or have the caller pass
in the type token for the element type and create an array of the right
size, but be subjected to the "unchecked" warning. Or you could do as
the collection classes do and combine both: Take in an array, but if it
is the wrong size dynamically create another one, and get the warning.
While we normally advise that you avoid "unchecked" warnings at all
[5]
For compilers that support "
unchecked
" as a warning type, this situation is an ideal candidate for use
Exercise 16.10
: Modify
Interpret
further to allow users to specify a
type and size of array to create; set and get the elements of that array;
and access fields and invoke methods on specific elements of the array.