JavaTM 2 Platform
Standard Ed. 6

java.lang.reflect
类 Array

java.lang.Object
  继承者 java.lang.reflect.Array

public final class Array
extends Object

Array 类提供了动态创建和访问 Java 数组的方法。

Array 允许在执行 get 或 set 操作期间进行扩展转换,但如果发生收缩转换,则抛出 IllegalArgumentException


方法摘要
static Object get(Object array, int index)
          返回指定数组对象中索引组件的值。
static boolean getBoolean(Object array, int index)
          以 boolean 形式返回指定数组对象中索引组件的值。
static byte getByte(Object array, int index)
          以 byte 形式返回指定数组对象中索引组件的值。
static char getChar(Object array, int index)
          以 char 形式返回指定数组对象中索引组件的值。
static double getDouble(Object array, int index)
          以 double 形式返回指定数组对象中索引组件的值。
static float getFloat(Object array, int index)
          以 float 形式返回指定数组对象中索引组件的值。
static int getInt(Object array, int index)
          以 int 形式返回指定数组对象中索引组件的值。
static int getLength(Object array)
          以 int 形式返回指定数组对象的长度。
static long getLong(Object array, int index)
          以 long 形式返回指定数组对象中索引组件的值。
static short getShort(Object array, int index)
          以 short 形式返回指定数组对象中索引组件的值。
static Object newInstance(Class<?> componentType, int... dimensions)
          创建一个具有指定的组件类型和维度的新数组。
static Object newInstance(Class<?> componentType, int length)
          创建一个具有指定的组件类型和长度的新数组。
static void set(Object array, int index, Object value)
          将指定数组对象中索引组件的值设置为指定的新值。
static void setBoolean(Object array, int index, boolean z)
          将指定数组对象中索引组件的值设置为指定的 boolean 值。
static void setByte(Object array, int index, byte b)
          将指定数组对象中索引组件的值设置为指定的 byte 值。
static void setChar(Object array, int index, char c)
          将指定数组对象中索引组件的值设置为指定的 char 值。
static void setDouble(Object array, int index, double d)
          将指定数组对象中索引组件的值设置为指定的 double 值。
static void setFloat(Object array, int index, float f)
          将指定数组对象中索引组件的值设置为指定的 float 值。
static void setInt(Object array, int index, int i)
          将指定数组对象中索引组件的值设置为指定的 int 值。
static void setLong(Object array, int index, long l)
          将指定数组对象中索引组件的值设置为指定的 long 值。
static void setShort(Object array, int index, short s)
          将指定数组对象中索引组件的值设置为指定的 short 值。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

方法详细信息

newInstance

public static Object newInstance(Class<?> componentType,
                                 int length)
                          throws NegativeArraySizeException
创建一个具有指定的组件类型和长度的新数组。调用此方法等效于创建如下数组:
 int[] x = {length};
 Array.newInstance(componentType, x);
 

参数:
componentType - 表示新数组的组件类型的 Class 对象
length - 新数组的长度
返回:
新数组
抛出:
NullPointerException - 如果指定的 componentType 参数为 null
IllegalArgumentException - 如果 componentType 为 Void.TYPE
NegativeArraySizeException - 如果指定的 length 为负

newInstance

public static Object newInstance(Class<?> componentType,
                                 int... dimensions)
                          throws IllegalArgumentException,
                                 NegativeArraySizeException
创建一个具有指定的组件类型和维度的新数组。如果 componentType 表示一个非数组类或接口,则新数组具有 dimensions.length 维度,并且将 componentType 作为其组件类型。如果 componentType 表示一个数组类,则新数组的维数等于 dimensions.lengthcomponentType 的维数的总和。在这种情况下,新数组的组件类型为 componentType 的组件类型。

新数组的维数不能超过该实现所支持的数组维数(通常为 255)。

参数:
componentType - 表示新数组的组件类型的 Class 对象
dimensions - 表示新数组维度的 int 数组
返回:
新数组
抛出:
NullPointerException - 如果指定的 componentType 参数为 null
IllegalArgumentException - 如果指定的 dimensions 参数是一个零维度的数组,或者所请求的维数超过了该实现所支持的数组维数的限制(通常为 225),或者度的数组,或者所请求的维数超过了该实现所支持的数组维数的限制(通常为 225),或者 componentType 为 Void.TYPE
NegativeArraySizeException - 如果指定的 dimensions 参数中的任意组件为负。

getLength

public static int getLength(Object array)
                     throws IllegalArgumentException
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 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getByte

public static byte getByte(Object array,
                           int index)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException
byte 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getChar

public static char getChar(Object array,
                           int index)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException
char 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getShort

public static short getShort(Object array,
                             int index)
                      throws IllegalArgumentException,
                             ArrayIndexOutOfBoundsException
short 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getInt

public static int getInt(Object array,
                         int index)
                  throws IllegalArgumentException,
                         ArrayIndexOutOfBoundsException
int 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getLong

public static long getLong(Object array,
                           int index)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException
long 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getFloat

public static float getFloat(Object array,
                             int index)
                      throws IllegalArgumentException,
                             ArrayIndexOutOfBoundsException
float 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

getDouble

public static double getDouble(Object array,
                               int index)
                        throws IllegalArgumentException,
                               ArrayIndexOutOfBoundsException
double 形式返回指定数组对象中索引组件的值。

参数:
array - 数组
index - 索引
返回:
指定数组中索引组件的值
抛出:
NullPointerException - 如果指定对象为 null
IllegalArgumentException - 如果指定对象不是一个数组,或者无法通过一个恒等或扩展转换将索引元素转换为返回类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
get(java.lang.Object, int)

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 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setByte

public static void setByte(Object array,
                           int index,
                           byte b)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 byte 值。

参数:
array - 数组
index - 数组内部的索引
b - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setChar

public static void setChar(Object array,
                           int index,
                           char c)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 char 值。

参数:
array - 数组
index - 数组内部的索引
c - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setShort

public static void setShort(Object array,
                            int index,
                            short s)
                     throws IllegalArgumentException,
                            ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 short 值。

参数:
array - 数组
index - 数组内部的索引
s - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setInt

public static void setInt(Object array,
                          int index,
                          int i)
                   throws IllegalArgumentException,
                          ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 int 值。

参数:
array - 数组
index - 数组内部的索引
i - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setLong

public static void setLong(Object array,
                           int index,
                           long l)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 long 值。

参数:
array - 数组
index - 数组内部的索引
l - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setFloat

public static void setFloat(Object array,
                            int index,
                            float f)
                     throws IllegalArgumentException,
                            ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 float 值。

参数:
array - 数组
index - 数组内部的索引
f - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

setDouble

public static void setDouble(Object array,
                             int index,
                             double d)
                      throws IllegalArgumentException,
                             ArrayIndexOutOfBoundsException
将指定数组对象中索引组件的值设置为指定的 double 值。

参数:
array - 数组
index - 数组内部的索引
d - 索引组件的新值
抛出:
NullPointerException - 如果指定对象参数为 null
IllegalArgumentException - 如果指定对象参数不是一个数组,或者无法通过一个恒等或基本扩展转换将指定值转换为基础数组的指定类型
ArrayIndexOutOfBoundsException - 如果指定的 index 参数为负,或者它大于等于指定数组的长度
另请参见:
set(java.lang.Object, int, java.lang.Object)

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only