package java.lang.reflect;
/**
*
* 用来操作和生成数组的类,
* 没有实例,只有静态方法
*
* comment by liqiang
*
* @author Nakul Saraiya
*/
public final
class Array {
//private的构造函数,此类没有实例
private Array() {}
/**
*
* 动态创建一个数组
*
* 生成一个2维数组的例子:
* String[][] aa =
* (String[][]) Array.newInstance(new String[0].getClass(), 8);
* aa[0]= new String[2];
* aa[0][1] = "aa";
*
* @param 数组的元素类型,它同样可以是一个数组
* @param 数组长度
*/
public static Object newInstance(Class componentType, int length)
throws NegativeArraySizeException {
return newArray(componentType, length);
}
/**
* 创建一个多维数组
*
* 如果componentType不是数组,则新数组长度为dimensions.length
* 如果componentType是数组,则新数组的长度为componentType的维数加上
* dimensions.length
*
* 维数的最大值为255,超过则抛出IllegalArgumentException
* dimensions的长度为0也抛出IllegalArgumentException
*
*/
public static Object newInstance(Class componentType, int[] dimensions)
throws IllegalArgumentException, NegativeArraySizeException {
return multiNewArray(componentType, dimensions);
}
//获得数组的长度
public static native int getLength(Object array)
throws IllegalArgumentException;
//获得指定坐标的数组元素
public static native Object get(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的boolean型元素
public static native boolean getBoolean(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的byte型元素
public static native byte getByte(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的char型元素
public static native char getChar(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的short型元素
public static native short getShort(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的int型元素
public static native int getInt(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的long型元素
public static native long getLong(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的float型元素
public static native float getFloat(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//获得指定坐标的数组的double型元素
public static native double getDouble(Object array, int index)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的元素
//array表示将要操作的数组元素
//index表示坐标
//value表示设置的值
public static native void set(Object array, int index, Object value)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的boolean型元素
public static native void setBoolean(Object array, int index, boolean z)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的byte型元素
public static native void setByte(Object array, int index, byte b)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的char型元素
public static native void setChar(Object array, int index, char c)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的short型元素
public static native void setShort(Object array, int index, short s)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的int型元素
public static native void setInt(Object array, int index, int i)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的long型元素
public static native void setLong(Object array, int index, long l)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的float型元素
public static native void setFloat(Object array, int index, float f)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//设置数组的指定坐标的doublie型元素
public static native void setDouble(Object array, int index, double d)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
//创建数组
private static native Object newArray(Class componentType, int length)
throws NegativeArraySizeException;
//创建多维数组
private static native Object multiNewArray(Class componentType,
int[] dimensions)
throws IllegalArgumentException, NegativeArraySizeException;
}