Return multidimensional array in JNI

前端 未结 1 1654
谎友^
谎友^ 2021-01-28 12:26

I want to return an already existing multidimensional array from C++ to Java code. My code is as below:

int cells [3][3] = {{0}};

int[][]
Java_com_packagename_a         


        
相关标签:
1条回答
  • 2021-01-28 12:45
    int cells [3][3] = {{0}};
    
    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;
    }
    
    0 讨论(0)
提交回复
热议问题