zoukankan      html  css  js  c++  java
  • javajava.lang.reflect.Array

    public class TestArray01 {

        public static void main(String[] args) throws Exception {
            Class<?> classType = Class.forName("java.lang.String");
            
            //构造一个数组对象
            Object array = Array.newInstance(classType, 10);
            //为指定的数组对象的指定元素赋值
            Array.set(array, 5, "hello");
            
            String str = (String)Array.get(array, 5);
            
            System.out.println(array.toString());
            //Integer.TYPE返回的是int
            System.out.println(Integer.TYPE);
            //Integer.class返回的是Integer
            System.out.println(Integer.class);
        }
    }

    三维数组设值:

    import java.lang.reflect.Array;

    public class TestArray02 {

        public static void main(String[] args) {
            int[] dims = new int[]{5, 10, 15};
            //构造一个以dims数组维度的int类型的数组对象
            Object threeArray = Array.newInstance(Integer.TYPE, dims);
            System.out.println(threeArray instanceof int[][][]);
            
            //Returns the value of the indexed component in the specified array object.
            //The value is automatically wrapped in an object if it has a primitive type.
            Object twoArray = Array.get(threeArray, 3);
            
            System.out.println(twoArray instanceof int[][]);
            //Returns the Class representing the component type of an array.
            //If this class does not represent an array class this method returns null.
            //Class<?> classType = twoArray.getClass().getComponentType();
            
            Object oneArray = Array.get(twoArray, 5);
            System.out.println(oneArray instanceof int[]);
            
            Array.set(oneArray, 10, 37);
            
            int [][][] castarr = (int[][][])threeArray;
            
            System.out.println(castarr[3][5][10]);
            
        }
    }

  • 相关阅读:
    mysql 数据丢失更新的解决方法
    session springboot 会话共享实现
    java web cors-filter 跨域开源库springboot 配置
    redis 持久化数据保存RDB&AOF
    JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510) 解决
    springboot 配置启动非web applicationContexnt
    nginx session 配置失效解决
    sqlplus 的安装和配置
    文件下载在前端显示乱码的问题
    java Classpath 的解读
  • 原文地址:https://www.cnblogs.com/charleszhang1988/p/3051217.html
Copyright © 2011-2022 走看看