aspose file tools
The moose likes Java in General and the fly likes How to return an interger array from JNI to java layer? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to return an interger array from JNI to java layer?" Watch "How to return an interger array from JNI to java layer?" New topic
Author

How to return an interger array from JNI to java layer?

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
i have a native code, where i ll be filling up an array of integers. I want to return this to java layer.
Can i create a arraylist in java to get these back in java layer?

or anyother better way to do it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You could create the int array in Java code, then pass it as a parameter, or simply create the int array from native code.

JNI specification
Array operations


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
Thanks for the info.
But, is there a way or method to return a two dimensional array?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
There might be a two-dimensional array in C but there isn't in Java, only arrays of arrays.

Pass an int[][] as a parameter, or create an int[][] in the native code, almost exactly as Rob has told you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

An int[][] is actually an Object[] for which each element is int[]. You should mix Object arrays and int arrays in your native code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Thank you, Rob. Does that apply only in Java? I suppose there is no such thing as an Object[] in C/C++.

Did you miss out a "not" somewhere, maybe "not mix"?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Campbell Ritchie wrote:Thank you, Rob. Does that apply only in Java? I suppose there is no such thing as an Object[] in C/C++.

To be honest, I have no idea. I've done quite some C programming, but my C++ experience is lacking.

Did you miss out a "not" somewhere, maybe "not mix"?

I didn't, but my intentions seem to be missing.

What I was trying to say is, that Vinod should use NewObjectArray to create one Object array as the outer array (of type int[].class which is [I in native code), then use NewIntArray to create each int[] element, and finally use SetObjectArrayElement to set these as the values of the Object array.

In pseudo code:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Right, thank you, Rob. All explained. I am pretty sure there is not usually an Object in C, so there can't be an Object[], unless you create such a type yourself. Presumably as a struct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to return an interger array from JNI to java layer?
 
Similar Threads
Need a disconnected resultset
returning an array
is it possible to return an array of strucutres to java layer from JNI?
Is it possible to return an array of objects from a web service?
how to return a byte array from a native method to java?