Как возвратить массив от JNI до Java?

Эта ссылка шоу Вы определения уровней предупреждения (я принимаю Вас, используют код C# в Вашем веб-проекте). Уровень 4 является самым строгим.

  • 0: Выключает эмиссию всех предупреждающих сообщений.
  • 1: Отображает серьезные предупреждающие сообщения.
  • 2: предупреждения уровня 1 Дисплеев плюс определенный, менее - серьезные предупреждения, такие как предупреждения о скрывающихся участниках класса.
  • 3: предупреждения уровня 2 Дисплеев плюс определенный, менее - серьезные предупреждения, такие как предупреждения о выражениях, которые всегда оценивают к верный или ложь .
  • 4: Дисплеи все предупреждения уровня 3 плюс информационные предупреждения. Это - уровень предупреждения значения по умолчанию в командной строке.

120
задан Bill the Lizard 26 May 2011 в 17:49
поделиться

1 ответ

If you've examined the documentation and still have questions that should be part of your initial question. In this case, the JNI function in the example creates a number of arrays. The outer array is comprised of an 'Object' array creating with the JNI function NewObjectArray(). From the perspective of JNI, that's all a two dimensional array is, an object array containing a number of other inner arrays.

The following for loop creates the inner arrays which are of type int[] using the JNI function NewIntArray(). If you just wanted to return a single dimensional array of ints, then the NewIntArray() function is what you'd use to create the return value. If you wanted to create a single dimensional array of Strings then you'd use the NewObjectArray() function but with a different parameter for the class.

Since you want to return an int array, then your code is going to look something like this:

JNIEXPORT jintArray JNICALL Java_ArrayTest_initIntArray(JNIEnv *env, jclass cls, int size)
{
 jintArray result;
 result = (*env)->NewIntArray(env, size);
 if (result == NULL) {
     return NULL; /* out of memory error thrown */
 }
 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;
}
111
ответ дан 24 November 2019 в 01:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: