public final class Array
extends Object
java.lang.Object | |
↳ | java.lang.reflect.Array |
Array
类提供静态方法来动态创建和访问Java数组。
Array
允许在获取或设置操作期间扩展转换,但如果发生缩小转换则抛出 IllegalArgumentException
。
Public methods |
|
---|---|
static Object |
get(Object array, int index) 返回指定数组对象中索引组件的值。 |
static boolean |
getBoolean(Object array, int index) 以 |
static byte |
getByte(Object array, int index) 以 |
static char |
getChar(Object array, int index) 以 |
static double |
getDouble(Object array, int index) 以 |
static float |
getFloat(Object array, int index) 返回指定数组对象中索引组件的值,如 |
static int |
getInt(Object array, int index) 以 |
static int |
getLength(Object array) 以 |
static long |
getLong(Object array, int index) 以 |
static short |
getShort(Object array, int index) 以 |
static Object |
newInstance(Class<?> componentType, int length) 用指定的组件类型和长度创建一个新数组。 |
static Object |
newInstance(Class<?> componentType, int... dimensions) 用指定的组件类型和尺寸创建一个新数组。 |
static void |
set(Object array, int index, Object value) 将指定数组对象的索引组件的值设置为指定的新值。 |
static void |
setBoolean(Object array, int index, boolean value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setByte(Object array, int index, byte value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setChar(Object array, int index, char value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setDouble(Object array, int index, double value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setFloat(Object array, int index, float value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setInt(Object array, int index, int value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setLong(Object array, int index, long value) 将指定数组对象的索引组件的值设置为指定的 |
static void |
setShort(Object array, int index, short value) 将指定数组对象的索引组件的值设置为指定的 |
Inherited methods |
|
---|---|
From class java.lang.Object
|
Object get (Object array, int index)
返回指定数组对象中索引组件的值。 如果该对象具有基本类型,则该值将自动包装在对象中。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
Object |
the (possibly wrapped) value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
boolean getBoolean (Object array, int index)
以 boolean
返回指定数组对象中索引组件的值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
boolean |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
byte getByte (Object array, int index)
以 byte
返回指定数组对象中索引组件的值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
byte |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
char getChar (Object array, int index)
以 char
返回指定数组对象中索引组件的值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
char |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
double getDouble (Object array, int index)
返回指定数组对象中索引组件的值,如 double
。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
double |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
float getFloat (Object array, int index)
以 float
返回指定数组对象中索引组件的值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
float |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
int getInt (Object array, int index)
以 int
返回指定数组对象中索引组件的值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
int |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
int getLength (Object array)
以 int
返回指定数组对象的 int
。
Parameters | |
---|---|
array |
Object : the array |
Returns | |
---|---|
int |
the length of the array |
Throws | |
---|---|
IllegalArgumentException |
if the object argument is not an array |
long getLong (Object array, int index)
以 long
返回指定数组对象中索引组件的值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
long |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
short getShort (Object array, int index)
返回指定数组对象中索引组件的值,如 short
。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index |
Returns | |
---|---|
short |
the value of the indexed component in the specified array |
Throws | |
---|---|
NullPointerException |
If the specified object is null |
IllegalArgumentException |
If the specified object is not an array, or if the indexed element cannot be converted to the return type by an identity or widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
Object newInstance (Class<?> componentType, int length)
用指定的组件类型和长度创建一个新数组。 调用这个方法等同于创建一个数组,如下所示:
int[] x = {length}; Array.newInstance(componentType, x);
Parameters | |
---|---|
componentType |
Class : the Class object representing the component type of the new array |
length |
int : the length of the new array |
Returns | |
---|---|
Object |
the new array |
Throws | |
---|---|
NullPointerException |
if the specified componentType parameter is null |
IllegalArgumentException |
if componentType is TYPE |
NegativeArraySizeException |
if the specified length is negative |
Object newInstance (Class<?> componentType, int... dimensions)
用指定的组件类型和尺寸创建一个新数组。 如果componentType
表示非阵列类或接口,新阵列具有dimensions.length
尺寸和componentType
作为其成分的类型。 如果componentType
表示一个数组类,则新数组的维数等于总和dimensions.length
和维数componentType
。 在这种情况下,新阵列的组件类型是组件类型componentType
。
新数组的维数不得超过实现支持的数组维数(通常为255)。
Parameters | |
---|---|
componentType |
Class : the Class object representing the component type of the new array |
dimensions |
int : an array of int representing the dimensions of the new array |
Returns | |
---|---|
Object |
the new array |
Throws | |
---|---|
NullPointerException |
if the specified componentType argument is null |
IllegalArgumentException |
if the specified dimensions argument is a zero-dimensional array, or if the number of requested dimensions exceeds the limit on the number of array dimensions supported by the implementation (typically 255), or if componentType is TYPE . |
NegativeArraySizeException |
if any of the components in the specified dimensions argument is negative. |
void set (Object array, int index, Object value)
将指定数组对象的索引组件的值设置为指定的新值。 如果数组具有原始组件类型,则新值将自动解包。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
Object : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the array component type is primitive and an unwrapping conversion fails |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
void setBoolean (Object array, int index, boolean value)
将指定数组对象的索引组件的值设置为指定的 boolean
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
boolean : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setByte (Object array, int index, byte value)
将指定数组对象的索引组件的值设置为指定的 byte
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
byte : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setChar (Object array, int index, char value)
将指定数组对象的索引组件的值设置为指定的 char
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
char : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setDouble (Object array, int index, double value)
将指定数组对象的索引组件的值设置为指定的 double
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
double : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setFloat (Object array, int index, float value)
将指定数组对象的索引组件的值设置为指定的 float
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
float : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setInt (Object array, int index, int value)
将指定数组对象的索引组件的值设置为指定的 int
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
int : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setLong (Object array, int index, long value)
将指定数组对象的索引组件的值设置为指定的 long
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
long : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看:
void setShort (Object array, int index, short value)
将指定数组对象的索引组件的值设置为指定的 short
值。
Parameters | |
---|---|
array |
Object : the array |
index |
int : the index into the array |
value |
short : the new value of the indexed component |
Throws | |
---|---|
NullPointerException |
If the specified object argument is null |
IllegalArgumentException |
If the specified object argument is not an array, or if the specified value cannot be converted to the underlying array's component type by an identity or a primitive widening conversion |
ArrayIndexOutOfBoundsException |
If the specified index argument is negative, or if it is greater than or equal to the length of the specified array |
也可以看看: