Package java.lang.reflect
Class Array
java.lang.Object
java.lang.reflect.Array
public final class Array extends Object
Provides static methods to create and access arrays dynamically.
-
Method Summary
Modifier and Type Method Description static Objectget(Object array, int index)Returns the element of the array at the specified index.static booleangetBoolean(Object array, int index)Returns the boolean at the given index in the given boolean array.static bytegetByte(Object array, int index)Returns the byte at the given index in the given byte array.static chargetChar(Object array, int index)Returns the char at the given index in the given char array.static doublegetDouble(Object array, int index)Returns the double at the given index in the given array.static floatgetFloat(Object array, int index)Returns the float at the given index in the given array.static intgetInt(Object array, int index)Returns the int at the given index in the given array.static intgetLength(Object array)Returns the length of the array.static longgetLong(Object array, int index)Returns the long at the given index in the given array.static shortgetShort(Object array, int index)Returns the short at the given index in the given array.static ObjectnewInstance(Class<?> componentType, int size)Returns a new array of the specified component type and length.static ObjectnewInstance(Class<?> componentType, int... dimensions)Returns a new multidimensional array of the specified component type and dimensions.static voidset(Object array, int index, Object value)Sets the element of the array at the specified index to the value.static voidsetBoolean(Object array, int index, boolean value)Setsarray[index] = value.static voidsetByte(Object array, int index, byte value)Setsarray[index] = value.static voidsetChar(Object array, int index, char value)Setsarray[index] = value.static voidsetDouble(Object array, int index, double value)Setsarray[index] = value.static voidsetFloat(Object array, int index, float value)Setsarray[index] = value.static voidsetInt(Object array, int index, int value)Setsarray[index] = value.static voidsetLong(Object array, int index, long value)Setsarray[index] = value.static voidsetShort(Object array, int index, short value)Setsarray[index] = value.
-
Method Details
-
get
public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the element of the array at the specified index. Equivalent toarray[index]. If the array component is a primitive type, the result is automatically boxed.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an arrayArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getBoolean
public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the boolean at the given index in the given boolean array.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getByte
public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the byte at the given index in the given byte array.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getChar
public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the char at the given index in the given char array.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getDouble
public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the double at the given index in the given array. Applies to byte, char, float, double, int, long, and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getFloat
public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the float at the given index in the given array. Applies to byte, char, float, int, long, and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getInt
public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the int at the given index in the given array. Applies to byte, char, int, and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getLength
Returns the length of the array. Equivalent toarray.length.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array
-
getLong
public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the long at the given index in the given array. Applies to byte, char, int, long, and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
getShort
public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionReturns the short at the given index in the given array. Applies to byte and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the element at the index position can not be converted to the return typeArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws NegativeArraySizeException, IllegalArgumentExceptionReturns a new multidimensional array of the specified component type and dimensions. Equivalent tonew componentType[d0][d1]...[dn]for a dimensions array of { d0, d1, ... , dn }.- Throws:
NullPointerException- ifarray == nullNegativeArraySizeException- if any of the dimensions are negativeIllegalArgumentException- 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 NegativeArraySizeExceptionReturns a new array of the specified component type and length. Equivalent tonew componentType[size].- Throws:
NullPointerException- if the component type is nullNegativeArraySizeException- ifsize < 0
-
set
public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSets the element of the array at the specified index to the value. Equivalent toarray[index] = value. If the array component is a primitive type, the value is automatically unboxed.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- ifarrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setBoolean
Setsarray[index] = value. Applies to boolean arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setByte
public static void setByte(Object array, int index, byte value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to byte, double, float, int, long, and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setChar
public static void setChar(Object array, int index, char value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to char, double, float, int, and long arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setDouble
public static void setDouble(Object array, int index, double value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to double arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setFloat
public static void setFloat(Object array, int index, float value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to double and float arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setInt
public static void setInt(Object array, int index, int value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to double, float, int, and long arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setLong
public static void setLong(Object array, int index, long value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to double, float, and long arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-
setShort
public static void setShort(Object array, int index, short value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionSetsarray[index] = value. Applies to double, float, int, long, and short arrays.- Throws:
NullPointerException- ifarray == nullIllegalArgumentException- if thearrayis not an array or the value cannot be converted to the array type by a widening conversionArrayIndexOutOfBoundsException- ifindex < 0 || index >= array.length
-