我想将已存在的多维数组从C ++返回到Java代码。我的代码如下:
int cells [3][3] = {{0}};
int[][]
Java_com_packagename_appname_GameView_addNew(
JNIEnv *env,
jobject, int row, int col, int val) {
cells[row][col] = val;
return cells;
}
但显然它没有用。什么是正确的方法?
答案 0 :(得分:2)
int cells [3][3] = ;
jobjectArray
Java_com_packagename_appname_GameView_addNew(
JNIEnv *env,
jobject, int row, int col, int val) {
cells[row][col] = val;
// Get the int array class
jclass cls = env->FindClass("[I");
jintArray iniVal = env->NewIntArray(3);
// Create the returnable jobjectArray with an initial value
jobjectArray outer = env->NewObjectArray(3,cls, iniVal);
for (int i = 0; i < 3; i++)
{
jintArray inner = env->NewIntArray(3);
env->SetIntArrayRegion(inner, 0, 3, cells[i]);
// set inner's values
env->SetObjectArrayElement(outer, i, inner);
env->DeleteLocalRef(inner);
}
return outer;
}