Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.lang.reflect

Class Array

java.lang.Object
|
+--java.lang.reflect.Array


public final class Array

extends 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:See Also:

Method Summary

static java.lang.Objectget(java.lang.Object array, int index)

Gets an element of an array.
static booleangetBoolean(java.lang.Object array, int index)

Gets an element of a boolean array.
static bytegetByte(java.lang.Object array, int index)

Gets an element of a byte array.
static chargetChar(java.lang.Object array, int index)

Gets an element of a char array.
static doublegetDouble(java.lang.Object array, int index)

Gets an element of a double array.
static floatgetFloat(java.lang.Object array, int index)

Gets an element of a float array.
static intgetInt(java.lang.Object array, int index)

Gets an element of an int array.
static intgetLength(java.lang.Object array)

Gets the array length.
static longgetLong(java.lang.Object array, int index)

Gets an element of a long array.
static shortgetShort(java.lang.Object array, int index)

Gets an element of a short array.
static java.lang.ObjectnewInstance(java.lang.Class componentType, int length)

Creates a new single-dimensioned array.
static java.lang.ObjectnewInstance(java.lang.Class componentType, int[] dimensions)

Creates a new multi-dimensioned array.
static voidset(java.lang.Object array, int index, java.lang.Object value)

Sets an element of an array.
static voidsetBoolean(java.lang.Object array, int index, boolean value)

Sets an element of a boolean array.
static voidsetByte(java.lang.Object array, int index, byte value)

Sets an element of a byte array.
static voidsetChar(java.lang.Object array, int index, char value)

Sets an element of a char array.
static voidsetDouble(java.lang.Object array, int index, double value)

Sets an element of a double array.
static voidsetFloat(java.lang.Object array, int index, float value)

Sets an element of a float array.
static voidsetInt(java.lang.Object array, int index, int value)

Sets an element of an int array.
static voidsetLong(java.lang.Object array, int index, long value)

Sets an element of a long array.
static voidsetShort(java.lang.Object array, int index, short value)

Sets an element of a short array.

Method Details

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:

Throws:

See Also:


getBoolean

public static boolean getBoolean(java.lang.Object array, int index)

Gets an element of a boolean array.

Parameters:

Returns:

Throws:

See Also:


getByte

public static byte getByte(java.lang.Object array, int index)

Gets an element of a byte array.

Parameters:

Returns:

Throws:

See Also:


getChar

public static char getChar(java.lang.Object array, int index)

Gets an element of a char array.

Parameters:

Returns:

Throws:

See Also:


getDouble

public static double getDouble(java.lang.Object array, int index)

Gets an element of a double array.

Parameters:

Returns:

Throws:

See Also:


getFloat

public static float getFloat(java.lang.Object array, int index)

Gets an element of a float array.

Parameters:

Returns:

Throws:

See Also:


getInt

public static int getInt(java.lang.Object array, int index)

Gets an element of an int array.

Parameters:

Returns:

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:

Throws:

See Also:


getShort

public static short getShort(java.lang.Object array, int index)

Gets an element of a short array.

Parameters:

Returns:

Throws:

See Also:


newInstance

public static Object newInstance(java.lang.Class componentType, int length)

Creates a new single-dimensioned array.

Parameters:

Returns:

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:

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: