What is the purpose of java.lang.reflect.Array's getter and setter methods?

前端 未结 3 1644
执笔经年
执笔经年 2021-02-09 18:03

Java Class java.lang.reflect.Array provides a set of tools for creating an array dynamically. However in addition to that it has a whole set of methods for accessing (get, set,

3条回答
  •  面向向阳花
    2021-02-09 18:51

    Regarding the set...() methods, they do provide some use:

    Object a = new byte[1], b = new short[1], c = new int[1], d = new long[1];
    Array.setByte(a, 0, (byte)1);
    Array.setByte(b, 0, (byte)1);
    Array.setByte(c, 0, (byte)1);
    Array.setByte(d, 0, (byte)1);
    

    You don't need to know whether the array you're dealing with is an int[] or a long[] to set a value to 5, for example.

    Edit I changed the example to hopefully demonstrate the fact that set...() methods allow you to not necessarily know the primitive array's type statically.

提交回复
热议问题