集册 Java实例教程 从反射获取数组的长度

从反射获取数组的长度

欢马劈雪     最近更新时间:2020-01-02 10:19:05

486
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从反射获取数组的长度
/** from N o w  J a v a  . c o m**/

import java.lang.reflect.Array;

import static java.lang.System.out;


public class ArrayTrouble {

    public static void main(String... args) {

        Object o = Array.newInstance(int.class, 0);

        int[] i = (int[]) o;

        int[] j = new int[0];

        out.format("i.length = %d, j.length = %d, args.length = %d%n",

                i.length, j.length, args.length);

        Array.getInt(o, 0); // ArrayIndexOutOfBoundsException

    }

}


展开阅读全文