Question

I have a java class, Prime, which I have successfully 'compiled' into a python module with jcc with this command:

sudo python -m jcc --jar ../workspace/prime.jar --python prime --build --install

Prime.factorLists returns a multidimensional array; when I call it from python I get a Jarray, but it's full of Objects which I'm having trouble treating as arrays:

>>> Prime.factorLists(3)
JArray<object>[<Object: [I@7bd63e39>, <Object: [I@2e8f4fb3>, <Object: [I@42b988a6>, <Object: [I@22ba6c83>]

Other methods returning single-dimensional arrays do work:

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]

I want to use java.util.Arrays.toString to convert the arrays acting like Objects to strings and print them out, but I can't figure out how to package that into my python module. Alternatively, can I convert them to python lists or JArrays somehow?

Était-ce utile?

La solution

To convert the Object to a JArray_int, use the JArray_int.cast_  method:

>>> 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]]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top