Java中的数组类Array

来源:互联网 发布:淘宝评语怎么删除 编辑:程序博客网 时间:2024/05/24 05:44

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 0
原创粉丝点击