集册 Java实例教程 使用反射设置数组元素值

使用反射设置数组元素值

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

405
使用反射设置数组元素值

import java.lang.reflect.Array;

import static java.lang.System.err;
/* 
 来自 
*n o w j a v a . c o m - 时代Java*/


public class ArrayTroubleAgain {

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

        Integer[] ary = new Integer[2];

        try {

            Array.setInt(ary, 0, 1); // IllegalArgumentException


            // production code should handle these exceptions more gracefully

        } catch (IllegalArgumentException x) {

            err.format("Unable to box%n");

        } catch (ArrayIndexOutOfBoundsException x) {

            x.printStackTrace();

        }

    }
    /* 
    *来 自
     N o w J a v a . c o m - 时代Java
    */

}


展开阅读全文