Java中的数组类Array

java.lang.reflect.Array里面提供了动态创建和访问数组的静态类。

下面我们来看看它拥有哪些静态方法可以使用。

1、getXXX函数

用来获取指定数组、指定索引的所对应的值。

这里写图片描述

public static Object get(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    if (array instanceof Object[])
        return ((Object[]) array)[index];

    if (array instanceof boolean[])
        return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE;

    if (array instanceof byte[])
        return Byte.valueOf(((byte[]) array)[index]);

    if (array instanceof char[])
        return Character.valueOf(((char[]) array)[index]);

    if (array instanceof short[])
        return Short.valueOf(((short[]) array)[index]);

    if (array instanceof int[])
        return Integer.valueOf(((int[]) array)[index]);

    if (array instanceof long[])
        return Long.valueOf(((long[]) array)[index]);

    if (array instanceof float[])
        return new Float(((float[]) array)[index]);

    if (array instanceof double[])
        return new Double(((double[]) array)[index]);

    if (array == null)
        throw new NullPointerException();

    throw new IllegalArgumentException("Not an array");
}

2、创建一个数组

创建一个指定类型,指定维数的数组

这里写图片描述

public static Object newInstance(Class<?> componentType, int size)
        throws NegativeArraySizeException {
    if (!componentType.isPrimitive())
        return createObjectArray(componentType, size);

    if (componentType == Boolean.TYPE)
        return new boolean[size];

    if (componentType == Byte.TYPE)
        return new byte[size];

    if (componentType == Character.TYPE)
        return new char[size];

    if (componentType == Short.TYPE)
        return new short[size];

    if (componentType == Integer.TYPE)
        return new int[size];

    if (componentType == Long.TYPE)
        return new long[size];

    if (componentType == Float.TYPE)
        return new float[size];

    if (componentType == Double.TYPE)
        return new double[size];

    if (componentType == Void.TYPE)
        throw new IllegalArgumentException();

    throw new RuntimeException(); // should be impossible
}

3、SetXXX函数

用来设置指定数组、指定索引的元素值。

这里写图片描述

public static void set(Object array, int index, Object value)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    if (!array.getClass().isArray()) {
        throw new IllegalArgumentException("Not an array type");
    }

    if (array instanceof Object[]) {
        if (value != null &&
            !array.getClass().getComponentType().isInstance(value)) {
            // incompatible object type for this array
            throw new IllegalArgumentException("Wrong array type");
        }

        ((Object[]) array)[index] = value;
    } else {
        if (value == null) {
            throw new IllegalArgumentException("Primitive array can't take null values.");
        }

        if (value instanceof Boolean)
            setBoolean(array, index, ((Boolean) value).booleanValue());
        else if (value instanceof Byte)
            setByte(array, index, ((Byte) value).byteValue());
        else if (value instanceof Character)
            setChar(array, index, ((Character) value).charValue());
        else if (value instanceof Short)
            setShort(array, index, ((Short) value).shortValue());
        else if (value instanceof Integer)
            setInt(array, index, ((Integer) value).intValue());
        else if (value instanceof Long)
            setLong(array, index, ((Long) value).longValue());
        else if (value instanceof Float)
            setFloat(array, index, ((Float) value).floatValue());
        else if (value instanceof Double)
            setDouble(array, index, ((Double) value).doubleValue());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值