[英]Different kind of multi-dimensional arrays in Java?
I'm using Java 11 (+ IntelliJ IDE).我正在使用 Java 11(+ IntelliJ IDE)。 My code retrieves a multi-dimensional array through JNI from a 3d-party C++ library.
我的代码通过 JNI 从 3d 方 C++ 库中检索多维数组。
My code looks something like below:我的代码如下所示:
Object[][][] param1 = {{{"somevalue"}}};
Object[] param2 = param1
Object[][][] param3 = (Object[][][])param2; //OK
Object[][][] result1 = SomeNativeLibrary.someNativeCall(param1);
Object someResult = result1[0][0][4]; //OK
Object[] result2 = result1;
Object[][][] result3 = (Object[][][])result2; //CastException
I can clearly see through my debugger that the structure of param1 and result1 are different (even though both are assumed to be three-dimensional arrays):通过我的调试器我可以清楚地看到 param1 和 result1 的结构是不同的(即使两者都被假定为三维数组):
for param1: {Object[1][][]} -> {Object[1][]} -> {Object{1}}
对于 param1:
{Object[1][][]} -> {Object[1][]} -> {Object{1}}
for result1: {Object[1]} -> {Object[1]} -> {Object[4]}
结果1:
{Object[1]} -> {Object[1]} -> {Object[4]}
Even though casting from param2 to param3 works without problem, casting from result2 to result3 fails with即使从 param2 到 param3 的转换没有问题,从 result2 到 result3 的转换也失败了
"java.lang.CastException: class [Ljava.lang.Object; cannot be case to class [[[Ljava.lang.Object" (notice the single square-bracket versus triple square-bracket before the "L").
The problem I'm facing is that I return result1 through some remoting framework.我面临的问题是我通过一些远程框架返回 result1 。 So I end up (on the client/calling-side of the remoting) with a one-dimensional array like above result2.
所以我最终(在远程处理的客户端/调用端)得到了一个像上面 result2 一样的一维数组。
1)How can I transform back result2 into a Object[][][] array so that I more readily access data? 1)如何将 result2 转换回 Object[][][] 数组,以便更方便地访问数据?
2)Why is casting from param2 to param3 working but not result2 to result3? 2)为什么从 param2 到 param3 的转换有效,但从 result2 到 result3 无效?
Look up the documentation of SomeNativeLibrary.someNativeCall(param1)
, and check what it actually returns.查看
SomeNativeLibrary.someNativeCall(param1)
的文档,并检查它实际返回的内容。 The CastException shows that it is declared as a one-dimensional array of objects in the class file, so you need to treat it as such. CastException 表明它在 class 文件中被声明为对象的一维数组,因此您需要这样对待它。
If it was a multidimensional C++ array originally, then it is likely that it is represented as a one-dimensional array in Java, with element (x,y,z)
at index x*Y*Z + y*Z + z
, where Y
is the dimension of the array in the y direction, and Z
the dimension in the z direction.如果原来是一个多维C++数组,那么它很可能在Java中表示为一个一维数组,元素
(x,y,z)
在索引x*Y*Z + y*Z + z
处,其中Y
是数组在 y 方向上的维度, Z
是在 z 方向上的维度。 But check the documentation to be sure, and use it in that manner.但是请检查文档以确保并以这种方式使用它。
A multi-dimensional array in Java is not structured like that, but as a one-dimensional array of pointers to other arrays. Java 中的多维数组不是这样构造的,而是作为指向其他 arrays 的指针的一维数组。 So you cannot directly cast it to a multi-dimensional Java array.
所以不能直接将其转换为多维 Java 数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.