|
JNI provides special functions (relative to the type) to access Java arrays.
This example returns the maximum of an int array.
JNIEXPORT jint JNICALL Java_JavaHowTo_max(JNIEnv * env, jclass obj, jintArray arr) {
int i;
jsize len = env->GetArrayLength(arr, max = -1;
jint *body = env->GetIntArrayElements(arr, 0);
for (max = body[0], i=1; i < len; i++)
if (max < body[i]) max = body[i];
env->ReleaseIntArrayElements(arr, body, 0);
return max;
}
The Java wrapper
class JavaHowTo {
public static native int max(int [] t);
static {
System.loadLibrary("javahowto");
}
}
|
The test program
class JNIJavaHowTo {
public static void main(String[] args) {
int [] myArray = {4, 7, 5, 9, 2, 0, 1};
System.out.println(JavaHowTo.max(myArray));
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.