java.lang.Object
java.lang.reflect.Array
Array类提供了静态方法来动态创建和访问Java数组。
Array允许在获取或设置操作期间进行扩展转换,但如果会发生缩小转换,则会抛出IllegalArgumentException异常。
- 自版本:
- 1.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object返回指定数组对象中索引组件的值。static booleangetBoolean(Object array, int index) 返回指定数组对象中索引组件的值,作为boolean类型。static byte返回指定数组对象中索引组件的值,作为byte类型。static char返回指定数组对象中索引组件的值,作为char类型。static double返回指定数组对象中索引组件的值,作为double类型。static float返回指定数组对象中索引组件的值,作为float类型。static int返回指定数组对象中索引组件的值,作为int类型。static int返回指定数组对象的长度,作为int类型。static long返回指定数组对象中索引组件的值,作为long类型。static short返回指定数组对象中索引组件的值,作为short类型。static ObjectnewInstance(Class<?> componentType, int length) 使用指定的组件类型和长度创建一个新数组。static ObjectnewInstance(Class<?> componentType, int... dimensions) 使用指定的组件类型和维度创建一个新数组。static void将指定数组对象的索引组件的值设置为指定的新值。static voidsetBoolean(Object array, int index, boolean z) 将指定数组对象的索引组件的值设置为指定的boolean值。static void将指定数组对象的索引组件的值设置为指定的byte值。static void将指定数组对象的索引组件的值设置为指定的char值。static void将指定数组对象的索引组件的值设置为指定的double值。static void将指定数组对象的索引组件的值设置为指定的float值。static void将指定数组对象的索引组件的值设置为指定的int值。static void将指定数组对象的索引组件的值设置为指定的long值。static void将指定数组对象的索引组件的值设置为指定的short值。
-
Method Details
-
newInstance
public static Object newInstance(Class<?> componentType, int length) throws NegativeArraySizeException 使用指定的组件类型和长度创建一个新数组。调用此方法等效于以下方式创建数组:int[] x = {length}; Array.newInstance(componentType, x);新数组的维度数量不能超过255。
- 参数:
-
componentType- 表示新数组的组件类型的Class对象 -
length- 新数组的长度 - 返回:
- 新数组
- 抛出:
-
NullPointerException- 如果指定的componentType参数为null -
IllegalArgumentException- 如果componentType为Void.TYPE,或者请求的数组实例的维度数量超过255。 -
NegativeArraySizeException- 如果指定的length为负数
-
newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws IllegalArgumentException, NegativeArraySizeException 使用指定的组件类型和维度创建一个新数组。如果componentType表示非数组类或接口,则新数组具有dimensions.length维度和componentType作为其组件类型。如果componentType表示数组类,则新数组的维度数量等于dimensions.length和componentType的维度数量之和。在这种情况下,新数组的组件类型是componentType的组件类型。新数组的维度数量不能超过255。
- 参数:
-
componentType- 表示新数组的组件类型的Class对象 -
dimensions- 表示新数组维度的int数组 - 返回:
- 新数组
- 抛出:
-
NullPointerException- 如果指定的componentType参数为null -
IllegalArgumentException- 如果指定的dimensions参数是零维数组,如果componentType为Void.TYPE,或者请求的数组实例的维度数量超过255。 -
NegativeArraySizeException- 如果指定的dimensions参数中的任何组件为负数。
-
getLength
返回指定数组对象的长度,作为int类型。- 参数:
-
array- 数组 - 返回:
- 数组的长度
- 抛出:
-
IllegalArgumentException- 如果对象参数不是数组
-
get
public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值。如果该值具有原始类型,则自动将其包装在对象中。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的(可能已包装)值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度
-
getBoolean
public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为boolean类型。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者无法通过标识或扩展转换将索引元素转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getByte
public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为byte类型。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者无法通过标识或扩展转换将索引元素转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getChar
public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为char类型。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者无法通过标识或扩展转换将索引元素转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getShort
public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为short类型。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者如果索引元素无法通过标识或扩展转换转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getInt
public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为一个int。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者如果索引元素无法通过标识或扩展转换转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getLong
public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为一个long。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者如果索引元素无法通过标识或扩展转换转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getFloat
public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为一个float。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者如果索引元素无法通过标识或扩展转换转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
getDouble
public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为一个double。- 参数:
-
array- 数组 -
index- 索引 - 返回:
- 指定数组中索引组件的值
- 抛出:
-
NullPointerException- 如果指定的对象为null -
IllegalArgumentException- 如果指定的对象不是数组,或者如果索引元素无法通过标识或扩展转换转换为返回类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
set
public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的新值。如果数组具有原始组件类型,则首先自动解包新值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
value- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者如果数组组件类型为原始类型且解包转换失败 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度
-
setBoolean
public static void setBoolean(Object array, int index, boolean z) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的boolean值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
z- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setByte
public static void setByte(Object array, int index, byte b) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的byte值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
b- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setChar
public static void setChar(Object array, int index, char c) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的char值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
c- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setShort
public static void setShort(Object array, int index, short s) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的short值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
s- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setInt
public static void setInt(Object array, int index, int i) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的int值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
i- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setLong
public static void setLong(Object array, int index, long l) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的long值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
l- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setFloat
public static void setFloat(Object array, int index, float f) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的float值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
f- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-
setDouble
public static void setDouble(Object array, int index, double d) throws IllegalArgumentException, ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的double值。- 参数:
-
array- 数组 -
index- 数组中的索引 -
d- 索引组件的新值 - 抛出:
-
NullPointerException- 如果指定的对象参数为null -
IllegalArgumentException- 如果指定的对象参数不是数组,或者指定的值无法通过标识或原始扩展转换为底层数组的组件类型 -
ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度 - 参见:
-