In Java, a multiple-dimension array of non-primitive objects, e.g. Integer[][] arr, is defined. How should one access the array arr in a C program via JNI?
public class Foo {
public static Integer[][] arr = {{0}, {1, 2}, {3, 4, 5}};
}
First, get the field ID:
jclass clazz = (*env)->FindClass(env, "fully/qualified/package/Foo");
jfieldID field = (*env)->GetFieldID(env, clazz, "arr", "[[Ljava/lang/Integer;" );
Then you'll need to use this to get the actual field. Supposing you have a jobject of type Foo called fooObj:
jobject arrObj = (*env)->GetObjectField(env, fooObj, field);
arr can be cast into a jObjectArray, and you can manipulate the array using the jni array functions. Documentation can be found here.
Since you have a 2D array of Integer objects, you will have to go through the usual means to get the primitive type from the Integer class.
Integer objects, they are not int's, and you can't just cast them to jint. You'll have to either change your array to the primitive type, or go through the usual means to get the primitive type from the Integer classAt present, it is impossible to directly transfer a multiple-dimension array of some non-primitive objects from a Java program to a C program.
A solution to this problem is to make a primitive version of the non-primitive multiple-dimension array, and to transfer the primitive multiple-dimension array from Java to C. Anyway, such a multiple-dimension array is transferred as a jobjectArray natively.
The outline of working on a 2-dimension array of integer numbers is as follows:
jobjectArray in native C program;GetObjectArrayElement to iterate each row;GetIntArrayElements function to iterate each cell, e.g. jint *val = (*env)->GetIntArrayElements(env, row, NULL);.Integer class as you would in Java, using jni
int a[][3] = {{0}, {1, 2}, {3, 4, 5}};Give it a try!!