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]);
            
        }
    }

  • 相关阅读:
    关于Log4j的初始化
    Golang-interface(四 反射)
    JavaScript学习总结-技巧、有用函数、简洁方法、编程细节
    玩转iOS开发
    小谈一下Java I/O
    [ACM] 最短路算法整理(bellman_ford , SPFA , floyed , dijkstra 思想,步骤及模板)
    已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)
    计数排序
    跟我学solr---吐槽一下,我的文章被抄袭啦
    Navicat11全系列激活工具和使用方法
  • 原文地址:https://www.cnblogs.com/charleszhang1988/p/3051217.html
Copyright © 2011-2022 走看看