java.lang.reflect.Array

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;


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值