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

  • 相关阅读:
    Python合集之文件操作(二)
    Python合集之文件操作(一)
    Python合集之异常(二)
    Python合集之异常(一)
    Python合集之模块(五)
    Visual Studio 配置额外工具 Windows Terminal 等
    CMakeList.txt
    alpha智能图像(全栈的进阶之路)
    位运算实现多状态控制
    缓存函数 memorize
  • 原文地址:https://www.cnblogs.com/charleszhang1988/p/3051217.html
Copyright © 2011-2022 走看看