• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info.
But, is there a way or method to return a two dimensional array?
 
Marshal
Posts: 74050
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 74050
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 74050
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic