我有一个java类Prime
,我使用此命令成功“编译”到带有jcc
的python模块中:
sudo python -m jcc --jar ../workspace/prime.jar --python prime --build --install
Prime.factorLists
返回一个多维数组;当我从python中调用它时,我得到一个Jarray
,但它充满了Object
s,我在处理数组时遇到了麻烦:
>>> Prime.factorLists(3)
JArray<object>[<Object: [I@7bd63e39>, <Object: [I@2e8f4fb3>, <Object: [I@42b988a6>, <Object: [I@22ba6c83>]
返回一维数组的其他方法做工作:
Prime.generatePrimes(200,1)
JArray<int>[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
我想使用java.util.Arrays.toString
将类似Objects
的数组转换为字符串并将其打印出来,但我无法弄清楚如何将其打包到我的python模块中。或者,我可以将它们转换为python列表或JArray
以某种方式吗?
答案 0 :(得分:0)
要将Object
转换为JArray_int
,请使用JArray_int.cast_
方法:
>>> jarr_class=some_method_that_returns_an_array_of_ints_in_java.__class__
>>> factor_lists=[jarr_class.cast_(i) for i in Prime.factorLists(10)]
>>> factor_lists
[JArray<int>[], JArray<int>[], JArray<int>[2], JArray<int>[3], JArray<int>[2, 2], JArray<int>[5], JArray<int>[2, 3], JArray<int>[7], JArray<int>[2, 2, 2], JArray<int>[3, 3], JArray<int>[2, 5]]