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
- Java中的数组类Array
- Java 自定义 的Array类,类似PHP中的数组
- 5.2数组中的Array类
- java array数组是不是类
- C#中的数组(Array)
- C#中的Array-数组
- swift中的数组Array
- Swift 中的数组 (Array)
- Java使用Array类创建多维数组
- Java 之 Array 数组
- 细说java数组Array
- Java 之 Array 数组
- java数组(array)
- java--数组(Array)
- java数组Array
- java -Array数组操作
- java-数组array
- java----数组 array
- javascript之数组操作
- JS淘宝商品广告效果
- Python的列表相关
- java使用Calendar制作万年历
- 第15周上机实践项目1 - 验证算法(1)
- Java中的数组类Array
- 是否二叉排序树?
- 【android】Activity
- android -Input Events
- 哈希表及其运算的实现
- java中的字符串操作
- Oracle不同表空间之间的数据迁移
- stm32串口通信
- C 字符串排序