java - 从Java中的多维数组获取未知数量的维度

标签 java arrays multidimensional-array dimensions

是否可以在 Java 中找到具有“先验”未知维数的数组的维数?也就是说,如果不知道多维矩阵的维数,如果可能的话,如何检索它?

最佳答案

Does anyone know if it is possible in Java to get the number of dimensions of an array with an 'a priori' unknown number of dimensions? That is, if one doesn't know the number of dimensions of a multidimensional matrix, how could it be retrieved, if possible?

我不确定我是否正确理解了您要完成的任务。如果你只想知道数组中有多少个元素,Anubian 的回答是正确的。但我的理解是,您想计算给定一般数组的维数。

public class Test {
    public static int getNumberOfDimensions(Class<?> type) {
        if (type.getComponentType() == null) {
            return 0;
        } else {
            return getNumberOfDimensions(type.getComponentType()) + 1;
        }
    }

    public static void main(String[] args) {
        System.out.println(getNumberOfDimensions(int[][][].class)   == 3);
        System.out.println(getNumberOfDimensions(int[][].class)     == 2);
        System.out.println(getNumberOfDimensions(int[][][][].class) == 4);
        System.out.println(getNumberOfDimensions(int.class)         == 0);
    }
}

如果这不是您要查找的内容,我会提示您:长度尺寸 之间存在差异。


更新:我认为这与我们被问到的问题完全无关,但 Nicola 在评论中问我:

This works perfectly, but what about if the number of dimensions is defined at run-time (for instance the user has to input the desired amount of dimensions)? How you could define and initialize the array?

解决方案在于一些基于光反射的黑客攻击:

import java.lang.reflect.Array;

public class Test {
    public static Class<?> getArrayType(Class<?> componentType, int dimensions) throws ClassNotFoundException {
        if (dimensions == 0) {
            return componentType;
        }

        String rawName = componentType.getName();
        switch (rawName) {
            case "byte":    rawName = "B"; break;
            case "char":    rawName = "C"; break;
            case "double":  rawName = "D"; break;
            case "float":   rawName = "F"; break;
            case "int":     rawName = "I"; break;
            case "long":    rawName = "J"; break;
            case "short":   rawName = "S"; break;
            case "boolean": rawName = "Z"; break;
            default:
                rawName = "L" + rawName + ";";
                break;
        }

        for (int i = 0; i < dimensions; i++) {
            rawName = "[" + rawName;
        }

        return Class.forName(rawName);
    }

    public static Object createArray(Class<?> componentType, int dimensions, int length) throws NegativeArraySizeException, ClassNotFoundException {
        if (dimensions == 0) {
            return null;
        }

        Object array = Array.newInstance(getArrayType(componentType, dimensions - 1), length);

        for (int i = 0; i < length; i++) {
            Array.set(array, i, createArray(componentType, dimensions - 1, length));
        }

        return array;
    }

    public static void main(String[] args) throws ClassNotFoundException {
        Object object = createArray(Integer.class, 3, 10);
        System.out.println(object.getClass());
    }
}

诀窍是使用给定的组件类型为 N 维数组构造一个类。如果我们知道类名是如何存储在最低级别的,我们就可以做到这一点。其余代码只是简单的无趣递归。

关于java - 从Java中的多维数组获取未知数量的维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230854/

相关文章:

php - 更改数组的索引名称

perl - 如何在 Perl 中打印二维数组?

php - 按文件名对 $_FILES 进行排序

java - 检查 null Boolean 是否为真导致异常

java - 如何从接口(interface)类型的实现中获取该接口(interface)未提供的特定方法?

javascript - 复制数组 --> 堆栈或堆溢出?

求和的 Python 多重处理

c++ - C 中的字符串连接问题

java - 用于 Spring Boot 应用程序中集成测试的内存数据库配置(HSQLDB)

java - 如何在java中对包含特殊字符的XML文档进行签名?