Java Reference
In-Depth Information
16.10. Arrays
An array is an object but has no membersthe implicit
length
"field" of an
array is not an actual field. Asking the
Class
object of an array for fields,
methods, or constructors will all yield empty arrays. To create arrays and
to
get
and
set
the values of elements stored in an array, you can use the
static methods of the
Array
class. You can create arrays with either of two
newInstance
methods.
public static Object
newInstance(Class<?> componentType, int length)
Returns a reference to a new array of the specified length and
with component type
componentType
.
public static Object
newInstance(Class<?> componentType, int[] dimen-
sions)
Returns a reference to a multidimensional array, with dimen-
sions as specified by the elements of the
dimensions
array and
with the component type
componentType
. If the
dimensions
array
is empty or has a length greater than the number of dimen-
sions allowed by the implementation (typically 255), an
Illeg-
alArgumentException
is thrown.
For primitive types, use the class literal to obtain the
Class
object. For
example, use
byte.class
to create a
byte
array. The statement
byte[] ba = (byte[]) Array.newInstance(byte.class, 13);
is equivalent to
byte[] ba = new byte[13];