java.lang.reflect
Class Array
java.lang.Object
|
+--java.lang.reflect.Array
public final class
Arrayextends
Object Array holds static helper functions that allow you to create and
manipulate arrays by reflection. Operations know how to perform widening
conversions, but throw IllegalArgumentException if you attempt
a narrowing conversion. Also, when accessing primitive arrays, this
class performs object wrapping and unwrapping as necessary.
Note: This class returns and accepts types as Classes, even
primitive types; there are Class types defined that represent each
different primitive type. They are java.lang.Boolean.TYPE,
java.lang.Byte.TYPE,
, also available as boolean.class,
byte.class
, etc. These are not to be confused with the
classes java.lang.Boolean, java.lang.Byte
, etc., which are
real classes. Note also that the shorthand Object[].class
is a convenient way to get array Classes.
Performance note: This class performs best when it does not have
to convert primitive types. The further along the chain it has to convert,
the worse performance will be. You're best off using the array as whatever
type it already is, and then converting the result. You will do even
worse if you do this and use the generic set() function.
Since:Authors:- John Keiser
- Eric Blake <ebb9@email.byu.edu>
See Also:
static java.lang.Object | get(java.lang.Object array, int index)
|
static boolean | getBoolean(java.lang.Object array, int index)
|
static byte | getByte(java.lang.Object array, int index)
|
static char | getChar(java.lang.Object array, int index)
|
static double | getDouble(java.lang.Object array, int index)
|
static float | getFloat(java.lang.Object array, int index)
|
static int | getInt(java.lang.Object array, int index)
|
static int | getLength(java.lang.Object array)
|
static long | getLong(java.lang.Object array, int index)
|
static short | getShort(java.lang.Object array, int index)
|
static java.lang.Object | newInstance(java.lang.Class componentType, int length)
|
static java.lang.Object | newInstance(java.lang.Class componentType, int[] dimensions)
|
static void | set(java.lang.Object array, int index, java.lang.Object value)
|
static void | setBoolean(java.lang.Object array, int index, boolean value)
|
static void | setByte(java.lang.Object array, int index, byte value)
|
static void | setChar(java.lang.Object array, int index, char value)
|
static void | setDouble(java.lang.Object array, int index, double value)
|
static void | setFloat(java.lang.Object array, int index, float value)
|
static void | setInt(java.lang.Object array, int index, int value)
|
static void | setLong(java.lang.Object array, int index, long value)
|
static void | setShort(java.lang.Object array, int index, short value)
|
get
public static Object get(java.lang.Object array, int index)
Gets an element of an array. Primitive elements will be wrapped in
the corresponding class type.
Parameters:
Returns:
- the element at
array[index]
Throws:
See Also:
getBoolean
public static boolean getBoolean(java.lang.Object array, int index)
Gets an element of a boolean array.
Parameters:
Returns:
- the boolean element at
array[index]
Throws:
See Also:
getByte
public static byte getByte(java.lang.Object array, int index)
Gets an element of a byte array.
Parameters:
Returns:
- the byte element at
array[index]
Throws:
See Also:
getChar
public static char getChar(java.lang.Object array, int index)
Gets an element of a char array.
Parameters:
Returns:
- the char element at
array[index]
Throws:
See Also:
getDouble
public static double getDouble(java.lang.Object array, int index)
Gets an element of a double array.
Parameters:
Returns:
- the double element at
array[index]
Throws:
See Also:
getFloat
public static float getFloat(java.lang.Object array, int index)
Gets an element of a float array.
Parameters:
Returns:
- the float element at
array[index]
Throws:
See Also:
getInt
public static int getInt(java.lang.Object array, int index)
Gets an element of an int array.
Parameters:
Returns:
- the int element at
array[index]
Throws:
See Also:
getLength
public static int getLength(java.lang.Object array)
Gets the array length.
Parameters:
Returns:
Throws:
getLong
public static long getLong(java.lang.Object array, int index)
Gets an element of a long array.
Parameters:
Returns:
- the long element at
array[index]
Throws:
See Also:
getShort
public static short getShort(java.lang.Object array, int index)
Gets an element of a short array.
Parameters:
Returns:
- the short element at
array[index]
Throws:
See Also:
newInstance
public static Object newInstance(java.lang.Class componentType, int length)
Creates a new single-dimensioned array.
Parameters:
Returns:
- the created array, cast to an Object
Throws:
newInstance
public static Object newInstance(java.lang.Class componentType, int[] dimensions)
Creates a new multi-dimensioned array. The new array has the same
component type as the argument class, and the number of dimensions
in the new array is the sum of the dimensions of the argument class
and the length of the argument dimensions. Virtual Machine limitations
forbid too many dimensions (usually 255 is the maximum); but even
50 dimensions of 2 elements in each dimension would exceed your memory
long beforehand!
Parameters:
Returns:
- the created array, cast to an Object
Throws:
set
public static void set(java.lang.Object array, int index, java.lang.Object value)
Sets an element of an array. If the array is primitive, then the new
value is unwrapped and widened.
Parameters:
Throws:
See Also:
setBoolean
public static void setBoolean(java.lang.Object array, int index, boolean value)
Sets an element of a boolean array.
Parameters:
Throws:
See Also:
setByte
public static void setByte(java.lang.Object array, int index, byte value)
Sets an element of a byte array.
Parameters:
Throws:
See Also:
setChar
public static void setChar(java.lang.Object array, int index, char value)
Sets an element of a char array.
Parameters:
Throws:
See Also:
setDouble
public static void setDouble(java.lang.Object array, int index, double value)
Sets an element of a double array.
Parameters:
Throws:
See Also:
setFloat
public static void setFloat(java.lang.Object array, int index, float value)
Sets an element of a float array.
Parameters:
Throws:
See Also:
setInt
public static void setInt(java.lang.Object array, int index, int value)
Sets an element of an int array.
Parameters:
Throws:
See Also:
setLong
public static void setLong(java.lang.Object array, int index, long value)
Sets an element of a long array.
Parameters:
Throws:
See Also:
setShort
public static void setShort(java.lang.Object array, int index, short value)
Sets an element of a short array.
Parameters:
Throws:
See Also:
Note: This class returns and accepts types as Classes, even primitive types; there are Class types defined that represent each different primitive type. They are
java.lang.Boolean.TYPE, java.lang.Byte.TYPE,
, also available asboolean.class, byte.class
, etc. These are not to be confused with the classesjava.lang.Boolean, java.lang.Byte
, etc., which are real classes. Note also that the shorthandObject[].class
is a convenient way to get array Classes.Performance note: This class performs best when it does not have to convert primitive types. The further along the chain it has to convert, the worse performance will be. You're best off using the array as whatever type it already is, and then converting the result. You will do even worse if you do this and use the generic set() function.