java.lang.Object
java.lang.reflect.Array
Array 类提供动态创建和访问 Java 数组的静态方法。
Array 允许在 get 或 set 操作期间发生扩大转换,但如果发生缩小转换则抛出 IllegalArgumentException。
- 自从:
- 1.1
-
方法总结
修饰符和类型方法描述static 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值。
-
方法详情
-
newInstance
public static Object newInstance(Class <?> componentType, int length) throws NegativeArraySizeException 创建具有指定组件类型和长度的新数组。调用此方法相当于创建一个数组,如下所示:int[] x = {length}; Array.newInstance(componentType, x);新数组的维数不得超过 255。
- 参数:
componentType- 代表新数组组件类型的Class对象length- 新数组的长度- 返回:
- 新数组
- 抛出:
NullPointerException- 如果指定的componentType参数为空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参数为空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- 如果指定对象为空IllegalArgumentException- 如果指定对象不是数组ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度
-
getBoolean
public static boolean getBoolean(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为boolean。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getByte
public static byte getByte(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为byte。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getChar
public static char getChar(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为char。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getShort
public static short getShort(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为short。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getInt
public static int getInt(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为int。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getLong
public static long getLong(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为long。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getFloat
public static float getFloat(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为float。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
getDouble
public static double getDouble(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 返回指定数组对象中索引组件的值,作为double。- 参数:
array- 数组index- 索引- 返回:
- 指定数组中索引组件的值
- 抛出:
NullPointerException- 如果指定对象为空IllegalArgumentException- 如果指定的对象不是数组,或者索引元素不能通过标识或扩大转换转换为返回类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
set
public static void set(Object array, int index, Object value) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的新值。如果数组具有原始组件类型,则首先自动解包新值。- 参数:
array- 数组index- 数组的索引value- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果数组组件类型是原始类型并且展开转换失败ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度
-
setBoolean
public static void setBoolean(Object array, int index, boolean z) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的boolean值。- 参数:
array- 数组index- 数组的索引z- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setByte
public static void setByte(Object array, int index, byte b) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的byte值。- 参数:
array- 数组index- 数组的索引b- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setChar
public static void setChar(Object array, int index, char c) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的char值。- 参数:
array- 数组index- 数组的索引c- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setShort
public static void setShort(Object array, int index, short s) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的short值。- 参数:
array- 数组index- 数组的索引s- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setInt
public static void setInt(Object array, int index, int i) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的int值。- 参数:
array- 数组index- 数组的索引i- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setLong
public static void setLong(Object array, int index, long l) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的long值。- 参数:
array- 数组index- 数组的索引l- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setFloat
public static void setFloat(Object array, int index, float f) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的float值。- 参数:
array- 数组index- 数组的索引f- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-
setDouble
public static void setDouble(Object array, int index, double d) throws IllegalArgumentException , ArrayIndexOutOfBoundsException 将指定数组对象的索引组件的值设置为指定的double值。- 参数:
array- 数组index- 数组的索引d- 索引组件的新值- 抛出:
NullPointerException- 如果指定的对象参数为空IllegalArgumentException- 如果指定的对象参数不是数组,或者如果指定的值无法通过标识或原始扩展转换转换为基础数组的组件类型ArrayIndexOutOfBoundsException- 如果指定的index参数为负数,或者大于或等于指定数组的长度- 参见:
-