to top
Android APIs
public final class

Array

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

Class Overview

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 boolean at the given index in the given boolean array.
static byte getByte(Object array, int index)
Returns the byte at the given index in the given byte array.
static char getChar(Object array, int index)
Returns the char at the given index in the given char array.
static double getDouble(Object array, int index)
Returns the double at the given index in the given array.
static float getFloat(Object array, int index)
Returns the float at the given index in the given array.
static int getInt(Object array, int index)
Returns the int at the given index in the given array.
static int getLength(Object array)
Returns the length of the array.
static long getLong(Object array, int index)
Returns the long at the given index in the given array.
static short getShort(Object array, int index)
Returns the short at the given index in the given array.
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 array[index] = value.
static void setByte(Object array, int index, byte value)
Sets array[index] = value.
static void setChar(Object array, int index, char value)
Sets array[index] = value.
static void setDouble(Object array, int index, double value)
Sets array[index] = value.
static void setFloat(Object array, int index, float value)
Sets array[index] = value.
static void setInt(Object array, int index, int value)
Sets array[index] = value.
static void setLong(Object array, int index, long value)
Sets array[index] = value.
static void setShort(Object array, int index, short value)
Sets array[index] = 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. Equivalent to array[index]. If the array component is a primitive type, the result is automatically boxed.

Throws
NullPointerException if array == 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 boolean at the given index in the given boolean array.

Throws
NullPointerException if array == 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 byte at the given index in the given byte array.

Throws
NullPointerException if array == 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 char at the given index in the given char array.

Throws
NullPointerException if array == 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 double at the given index in the given array. Applies to byte, char, float, double, int, long, and short arrays.

Throws
NullPointerException if array == 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 float at the given index in the given array. Applies to byte, char, float, int, long, and short arrays.

Throws
NullPointerException if array == 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 int at the given index in the given array. Applies to byte, char, int, and short arrays.

Throws
NullPointerException if array == 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. Equivalent to array.length.

Throws
NullPointerException if array == null
IllegalArgumentException if array is not an array

public static long getLong (Object array, int index)

Since: API Level 1

Returns the long at the given index in the given array. Applies to byte, char, int, long, and short arrays.

Throws
NullPointerException if array == 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 short at the given index in the given array. Applies to byte and short arrays.

Throws
NullPointerException if array == 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. Equivalent to new componentType[size].

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. Equivalent to new componentType[d0][d1]...[dn] for a dimensions array of { d0, d1, ... , dn }.

Throws
NullPointerException if array == 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. Equivalent to array[index] = value. If the array component is a primitive type, the value is automatically unboxed.

Throws
NullPointerException if array == 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 array[index] = value. Applies to boolean arrays.

Throws
NullPointerException if array == 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 array[index] = value. Applies to byte, double, float, int, long, and short arrays.

Throws
NullPointerException if array == 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

Sets array[index] = value. Applies to char, double, float, int, and long arrays.

Throws
NullPointerException if array == 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

Sets array[index] = value. Applies to double arrays.

Throws
NullPointerException if array == 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

Sets array[index] = value. Applies to double and float arrays.

Throws
NullPointerException if array == 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

Sets array[index] = value. Applies to double, float, int, and long arrays.

Throws
NullPointerException if array == 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

Sets array[index] = value. Applies to double, float, and long arrays.

Throws
NullPointerException if array == 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

Sets array[index] = value. Applies to double, float, int, long, and short arrays.

Throws
NullPointerException if array == 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