如果你已经阅读了文档,但仍然有一些问题应作为最初问题的一部分。在这种情况下,示例中的JNI函数将创建多个数组。外部数组由使用JNI函数创建的“对象”数组组成
NewObjectArray()。从JNI的角度来看,这就是一个二维数组,即一个包含多个其他内部数组的对象数组。
下面的for循环使用JNI函数创建int []类型的内部数组
NewIntArray()。如果你只想返回一个一维整数数组,则
NewIntArray()可以使用该函数来创建返回值。如果要创建字符串的一维数组,则可以使用该
NewObjectArray()函数,但为该类使用不同的参数。
由于你要返回一个int数组,因此你的代码将如下所示:
JNIEXPORT jintArray JNICALL Java_ArrayTest_initIntArray(JNIEnv *env, jclass cls, int size){ jintArray result; result = (*env)->NewIntArray(env, size); if (result == NULL) { return NULL; } int i; // fill a temp structure to use to populate the java int array jint fill[size]; for (i = 0; i < size; i++) { fill[i] = 0; // put whatever logic you want to populate the values here. } // move from the temp structure to the java structure (*env)->SetIntArrayRegion(env, result, 0, size, fill); return result;}


