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

  • 相关阅读:
    vue相关坑
    jQuery上传文件按钮美化
    大屏幕数据可视化问题
    jquery实现点击页面空白处,弹框消失
    知识点
    js数字串传参时变科学计数法
    jQuery循环遍历取值
    如何判断一个弹框是打开还是关闭状态?
    js库写法
    React版本修改内容
  • 原文地址:https://www.cnblogs.com/charleszhang1988/p/3051217.html
Copyright © 2011-2022 走看看