public final class

Array

extends Object
java.lang.Object
   ↳ java.lang.reflect.Array

Class Overview

This class provides static methods to create and access arrays dynamically.

Summary

Public Methods
static Object get(Object array, int index)
Returns the element of the array at the specified index.
static boolean getBoolean(Object array, int index)
Returns the element of the array at the specified index, converted to a boolean, if possible.
static byte getByte(Object array, int index)
Returns the element of the array at the specified index, converted to a byte, if possible.
static char getChar(Object array, int index)
Returns the element of the array at the specified index, converted to a char, if possible.
static double getDouble(Object array, int index)
Returns the element of the array at the specified index, converted to a double, if possible.
static float getFloat(Object array, int index)
Returns the element of the array at the specified index, converted to a float, if possible.
static int getInt(Object array, int index)
Returns the element of the array at the specified index, converted to an int, if possible.
static int getLength(Object array)
Returns the length of the array.
static long getLong(Object array, int index)
Returns the element of the array at the specified index, converted to a long, if possible.
static short getShort(Object array, int index)
Returns the element of the array at the specified index, converted to a short, if possible.
static Object newInstance(Class<?> componentType, int size)
Returns a new array of the specified component type and length.
static Object newInstance(Class<?> componentType, int... dimensions)
Returns a new multidimensional array of the specified component type and dimensions.
static void set(Object array, int index, Object value)
Sets the element of the array at the specified index to the value.
static void setBoolean(Object array, int index, boolean value)
Sets the element of the array at the specified index to the boolean value.
static void setByte(Object array, int index, byte value)
Sets the element of the array at the specified index to the byte value.
static void setChar(Object array, int index, char value)
Set the element of the array at the specified index to the char value.
static void setDouble(Object array, int index, double value)
Set the element of the array at the specified index to the double value.
static void setFloat(Object array, int index, float value)
Set the element of the array at the specified index to the float value.
static void setInt(Object array, int index, int value)
Set the element of the array at the specified index to the int value.
static void setLong(Object array, int index, long value)
Set the element of the array at the specified index to the long value.
static void setShort(Object array, int index, short value)
Set the element of the array at the specified index to the short value.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static Object get (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index. This reproduces the effect of array[index]. If the array component is a primitive type, the result is automatically wrapped.

Parameters
array the array
index the index
Returns
  • the requested element, possibly wrapped
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static boolean getBoolean (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a boolean, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static byte getByte (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a byte, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static char getChar (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a char, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static double getDouble (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a double, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static float getFloat (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a float, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static int getInt (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to an int, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static int getLength (Object array)

Since: API Level 1

Returns the length of the array. This reproduces the effect of array.length

Parameters
array the array
Returns
  • the length of the array
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array

public static long getLong (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a long, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static short getShort (Object array, int index)

Since: API Level 1

Returns the element of the array at the specified index, converted to a short, if possible. This reproduces the effect of array[index]

Parameters
array the array
index the index
Returns
  • the requested element
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the element at the index position can not be converted to the return type
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static Object newInstance (Class<?> componentType, int size)

Since: API Level 1

Returns a new array of the specified component type and length. This reproduces the effect of new componentType[size].

Parameters
componentType the component type of the new array
size the length of the new array
Returns
  • the new array
Throws
NullPointerException if the component type is null
NegativeArraySizeException if size < 0

public static Object newInstance (Class<?> componentType, int... dimensions)

Since: API Level 9

Returns a new multidimensional array of the specified component type and dimensions. This reproduces the effect of new componentType[d0][d1]...[dn] for a dimensions array of { d0, d1, ... , dn }.

Parameters
componentType the component type of the new array
dimensions the dimensions of the new array
Returns
  • the new array
Throws
NullPointerException if the component type is null
NegativeArraySizeException if any of the dimensions are negative
IllegalArgumentException if the array of dimensions is of size zero, or exceeds the limit of the number of dimension for an array (currently 255)

public static void set (Object array, int index, Object value)

Since: API Level 1

Sets the element of the array at the specified index to the value. This reproduces the effect of array[index] = value. If the array component is a primitive type, the value is automatically unwrapped.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setBoolean (Object array, int index, boolean value)

Since: API Level 1

Sets the element of the array at the specified index to the boolean value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setByte (Object array, int index, byte value)

Since: API Level 1

Sets the element of the array at the specified index to the byte value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setChar (Object array, int index, char value)

Since: API Level 1

Set the element of the array at the specified index to the char value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setDouble (Object array, int index, double value)

Since: API Level 1

Set the element of the array at the specified index to the double value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setFloat (Object array, int index, float value)

Since: API Level 1

Set the element of the array at the specified index to the float value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setInt (Object array, int index, int value)

Since: API Level 1

Set the element of the array at the specified index to the int value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setLong (Object array, int index, long value)

Since: API Level 1

Set the element of the array at the specified index to the long value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length

public static void setShort (Object array, int index, short value)

Since: API Level 1

Set the element of the array at the specified index to the short value. This reproduces the effect of array[index] = value.

Parameters
array the array
index the index
value the new value
Throws
NullPointerException if the array is null
IllegalArgumentException if the array is not an array or the value cannot be converted to the array type by a widening conversion
ArrayIndexOutOfBoundsException if index < 0 || index >= array.length