【问题标题】:How to find size of the 2D objectArray in JNI如何在 JNI 中查找 2D objectArray 的大小
【发布时间】:2017-03-03 11:12:39
【问题描述】:

我想在 JNI 的 2D objectArray 中找到行数和列数。我该怎么办?请任何人都可以帮助我解决这个问题。

【问题讨论】:

  • 使用GetArrayLength

标签: android-ndk java-native-interface


【解决方案1】:

如果你有数组数组,你必须通过对象数组。

java 中的 1d 数组是 JNI 中的 1d 基元数组 二维数组是对象数组

所以,对于这样的代码:

public class PassArray {
  public static native void passBooleanArray(boolean [][] array);

  public static void main(String[] args) {
    boolean [][] boolArray   = { { true, true, true }, {false, false, false} };
    passBooleanArray(boolArray);
  }
}

你需要这样的东西:

/* get size of the array */
jsize len = (*env)->GetArrayLength(env, array);

for(int i=0; i<len; i++) {

  /* get the array at following indexes of array; it will be referecende by C pointer */
  jbooleanArray body = (*env)->GetObjectArrayElement(env, array, i);
  jsize innerLen = (*env)->GetArrayLength(env, body);

  jboolean *booleanBody = (*env)->GetBooleanArrayElements(env, body, 0);
  for(int j=0; j<innerLen; j++) {
    /* do some stuff */
    printf("Boolean value: %s\n", booleanBody[j] == JNI_TRUE ? "true" : "false");
  }
  /* release body when you decide it is no longer needed */
  (*env)->ReleaseBooleanArrayElements(env, array, booleanBody, 0);
}

更新:

看这里:

https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo026

获取工作示例代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 2017-10-08
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    相关资源
    最近更新 更多