- java.lang.Object
-
- java.lang.reflect.Array
-
public final class Array extends Object
This class provides methods to dynamically create and access arrays.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Objectget(Object array, int index)Return the element of the array at the specified index.static booleangetBoolean(Object array, int index)Return the element of the array at the specified index, converted to a boolean if possible.static bytegetByte(Object array, int index)Return the element of the array at the specified index, converted to a byte if possible.static chargetChar(Object array, int index)Return the element of the array at the specified index, converted to a char if possible.static doublegetDouble(Object array, int index)Return the element of the array at the specified index, converted to a double if possible.static floatgetFloat(Object array, int index)Return the element of the array at the specified index, converted to a float if possible.static intgetInt(Object array, int index)Return the element of the array at the specified index, converted to an int if possible.static intgetLength(Object array)Return the length of the array.static longgetLong(Object array, int index)Return the element of the array at the specified index, converted to a long if possible.static shortgetShort(Object array, int index)Return the element of the array at the specified index, converted to a short if possible.static ObjectnewInstance(Class<?> componentType, int size)Return a new array of the specified component type and length.static ObjectnewInstance(Class<?> componentType, int... dimensions)Return a new multidimensional array of the specified component type and dimensions.static voidset(Object array, int index, Object value)Set the element of the array at the specified index to the value.static voidsetBoolean(Object array, int index, boolean value)Set the element of the array at the specified index to the boolean value.static voidsetByte(Object array, int index, byte value)Set the element of the array at the specified index to the byte value.static voidsetChar(Object array, int index, char value)Set the element of the array at the specified index to the char value.static voidsetDouble(Object array, int index, double value)Set the element of the array at the specified index to the double value.static voidsetFloat(Object array, int index, float value)Set the element of the array at the specified index to the float value.static voidsetInt(Object array, int index, int value)Set the element of the array at the specified index to the int value.static voidsetLong(Object array, int index, long value)Set the element of the array at the specified index to the long value.static voidsetShort(Object array, int index, short value)Set the element of the array at the specified index to the short value.
-
-
-
Method Detail
-
get
public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index. This reproduces the effect ofarray[index]If the array component is a base type, the result is automatically wrapped.- Parameters:
array- the arrayindex- the index- Returns:
- the requested element, possibly wrapped
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an arrayArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getBoolean
public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a boolean if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getByte
public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a byte if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getChar
public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a char if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getDouble
public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a double if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getFloat
public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a float if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getInt
public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to an int if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getLength
public static int getLength(Object array) throws IllegalArgumentException
Return the length of the array. This reproduces the effect ofarray.length- Parameters:
array- the array- Returns:
- the length
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array
-
getLong
public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a long if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
getShort
public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Return the element of the array at the specified index, converted to a short if possible. This reproduces the effect ofarray[index]- Parameters:
array- the arrayindex- the index- Returns:
- the requested element
- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the element cannot be converted to the requested type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws NegativeArraySizeException, IllegalArgumentException
Return a new multidimensional array of the specified component type and dimensions. This reproduces the effect ofnew componentType[d0][d1]...[dn]for a dimensions array of { d0, d1, ... , dn }- Parameters:
componentType- the component type of the new arraydimensions- the dimensions of the new array- Returns:
- the new array
- Throws:
NullPointerException- if the component type is nullNegativeArraySizeException- if any of the dimensions are negativeIllegalArgumentException- if componentType is Void.TYPE, or if the array of dimensions is of size zero, or exceeds the limit of the number of dimension for an array (currently 255)
-
newInstance
public static Object newInstance(Class<?> componentType, int size) throws NegativeArraySizeException
Return a new array of the specified component type and length. This reproduces the effect ofnew componentType[size]- Parameters:
componentType- the component type of the new arraysize- the length of the new array- Returns:
- the new array
- Throws:
NullPointerException- if the component type is nullNegativeArraySizeException- if the size if negativeIllegalArgumentException- if componentType is Void.TYPE, or the array dimension exceeds the limit of the number of dimension for an array (currently 255)
-
set
public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the value. This reproduces the effect ofarray[index] = valueIf the array component is a base type, the value is automatically unwrapped- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setBoolean
public static void setBoolean(Object array, int index, boolean value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the boolean value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setByte
public static void setByte(Object array, int index, byte value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the byte value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setChar
public static void setChar(Object array, int index, char value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the char value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setDouble
public static void setDouble(Object array, int index, double value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the double value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setFloat
public static void setFloat(Object array, int index, float value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the float value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setInt
public static void setInt(Object array, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the int value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setLong
public static void setLong(Object array, int index, long value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the long value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
setShort
public static void setShort(Object array, int index, short value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException
Set the element of the array at the specified index to the short value. This reproduces the effect ofarray[index] = value- Parameters:
array- the arrayindex- the indexvalue- the new value- Throws:
NullPointerException- if the array is nullIllegalArgumentException- if the array is not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- if the index is out of bounds -- negative or greater than or equal to the array length
-
-