微信
支付宝
# Java 数组反射 ## Java反射 - Java数组反射 我们可以使用Class类中的isArray()方法来检查类是否是数组。 我们可以创建一个数组,使用反射通过读取和修改其元素的值\` java.lang.reflect.Array \`类。 Array类的\` getLength()\`方法获取数组的长度。 Array类中的所有方法都是静态的。 要创建数组,请使用Array类中的重载静态方法newInstance()。 \`\`\` Object newInstance(Class componentType, int arrayLength) Object newInstance(Class componentType, int... dimensions) \`\`\` 第一个方法根据指定的组件类型和数组长度创建一个数组。 第二个版本创建指定组件类型和尺寸的数组。 newInstance()方法的返回类型是Object,我们需要将它转换为实际的数组类型。 下面的代码创建一个长度为5的\` int \`数组。 \`\`\` int\[\] ids = (int\[\])Array.newInstance(int.class, 5); \`\`\` 要创建一个维度为5乘3的int数组。 \`\`\` int\[\]\[\] matrix = (int\[\]\[\])Array.newInstance(int.class, 5, 3); \`\`\` ## 例子 以下代码显示了如何动态创建数组并操作其元素。 \`\`\` import java.lang.reflect.Array; public class Main { public static void main(String\[\] args) { try { Object my = Array.newInstance(int.class, 2); int n1 = Array.getInt(my, 0); int n2 = Array.getInt(my, 1); System.out.println("n1 = " + n1 + ", n2=" + n2); Array.set(my, 0, 11); Array.set(my, 1, 12); n1 = Array.getInt(my, 0); n2 = Array.getInt(my, 1); System.out.println("n1 = " + n1 + ", n2=" + n2); } catch (NegativeArraySizeException \| IllegalArgumentException \| ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); } } } \`\`\` 上面的代码生成以下结果。 !\[img\](https://atts.w3cschool.cn/attachments/jimg/java_reflection/EXAMPLE__176AC5BCB6D67F04F3D0.png) ## 获取数组的维度 Java支持array数组。 类中的\` getComponentType()\`方法返回数组的元素类型的Class对象。 以下代码说明了如何获取数组的维度。 \`\`\` public class Main { public static void main(String\[\] args) { int\[\]\[\]\[\] intArray = new int\[1\]\[2\]\[3\]; System.out.println("int\[\]\[\]\[\] dimension is " + getArrayDimension(intArray)); } public static int getArrayDimension(Object array) { int dimension = 0; Class c = array.getClass(); if (!c.isArray()) { throw new IllegalArgumentException("Object is not an array"); } while (c.isArray()) { dimension++; c = c.getComponentType(); } return dimension; } } \`\`\` 上面的代码生成以下结果。 !\[img\](https://atts.w3cschool.cn/attachments/jimg/java_reflection/GET_THE_DIMENSION_OF_AN_ARRAY__F0BEFA8CED246B00B0E5.png) ## 展开数组 Java数组是一个固定长度的数据结构。 要放大数组,我们可以创建一个更大尺寸的数组,并将旧数组元素复制到新数组元素。 以下代码显示如何使用反射展开数组。 \`\`\` import java.lang.reflect.Array; import java.util.Arrays; public class Main { public static void main(String\[\] args) { int\[\] ids = new int\[2\]; System.out.println(ids.length); System.out.println(Arrays.toString(ids)); ids = (int\[\]) expandBy(ids, 2); ids\[2\] = 3; System.out.println(ids.length); System.out.println(Arrays.toString(ids)); } public static Object expandBy(Object oldArray, int increment) { Object newArray = null; int oldLength = Array.getLength(oldArray); int newLength = oldLength + increment; Class c = oldArray.getClass(); newArray = Array.newInstance(c.getComponentType(), newLength); System.arraycopy(oldArray, 0, newArray, 0, oldLength); return newArray; } } \`\`\` 上面的代码生成以下结果。 !\[img\](https://atts.w3cschool.cn/attachments/jimg/java_reflection/EXPANDING_AN_ARRAY__D2CB284D6B9E972BDF45.png)
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Veylor
最近发布
常用SQL