将2D char数组从C函数返回到java JNI

时间:2017-04-19 09:33:53

标签: java c arrays

我试图在java中显示来自C函数的2D char数组的5x5字符网格。我目前使用的代码返回正确的5x5网格,但网格中的所有字符都显示为空符号或随机符号。我用来构造的代码是返回数组如下:

JNIEXPORT jobjectArray JNICALL Java_MapJNI_look(JNIEnv *env, jobject jObject, jint x, jint y){

initializeMap();

jobjectArray lookRow[5];
char lookChars[5][5];
char *arrayPointer;

int i, j, k, l;
for(i = 0; i < 5; i++){
    for(j = 0; j < 5; j++){

        int posX = x + j - 5/2;
        int posY = y + i - 5/2;

        if(posX >= 0 && posX < getMapWidth() && posY >= 0 && posY < getMapHeight()){
            lookChars[i][j] = map[posY][posX]; //todo check this is correct
        }else{
            lookChars[i][j] = 'X';
        }
    }
    arrayPointer = &lookChars[i][j];

    //Setting an element of the row array object to a particular sequence of map characters
    //5 represents the 5x5 look window
    lookRow[i] = createArrayRow(env, 5, arrayPointer);
}
//Creating an array that contains all the rows for the look window
//Any element of lookRow[] is valid when obtaining the class through GetObjectClass
jobjectArray rows = (*env)->NewObjectArray(env, 5, (*env)->GetObjectClass(env, lookRow[0]), 0);

for(k = 0; k < 5; k++){
    (*env)->SetObjectArrayElement(env,rows,k, lookRow[k]);
}

return rows; }

initializeMap()函数只是用'。'填充2D char数组。字符。 createArrayRow()函数如下:

static jobjectArray createArrayRow(JNIEnv *env, jsize count, char* elements){

  //Storing the class type for the object passed
  jclass stringClass = (*env)->FindClass(env, "java/lang/String");
  //Creating a jobjectArray out of the supplied information
  //This creates an array that can be passed back to java
  jobjectArray row = (*env)->NewObjectArray(env, count, stringClass, 0);
  jsize i;

  //Assigning each element of the newly created array object to a specific   string
  (*env)->SetObjectArrayElement(env, row, i, (*env)->NewStringUTF(env, elements));

return row; }

如果您有任何建议,我们将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

我采用的解决方案创建了一个字符数组,然后将每个数组合并在一起以创建一个2D jobjectArray字符数组。对象数组的每一行都是一个字符数组。

JNIEXPORT jobjectArray JNICALL Java_MyClass_get2DArray(JNIEnv *env, jobject jObject){

jchar singleArrayRow[10]; //Change 10 to the width of the 2D array
jcharArray arrayRows[10]; //Change 10 to the height of the 2D array

//Looping through the array and adding the relevant characters to an array 
int i, j, k, l;
for(i = 0; i < 10; i++){
    for(j = 0; j < 10; j++){
        //Using another array in order to passes the row of characters to createArrayRow function
        singleArrayRow[j] = arrayOfChars[i][j]; //arrayOfChars is just the previously specified char **arrayOfChars  
    }
    arrayRows[i] = createArrayRow(env, 10, singleArrayRow);
}
//Creating the object
jobjectArray rows = (*env)->NewObjectArray(env, 10, (*env)->GetObjectClass(env, arrayRows[0]), 0); //You can use any element of arrayRows[]

//Looping through the jobjectArray and aligning it each row object
for(i = 0; i < 10; i++){
    (*env)->SetObjectArrayElement(env, rows, i, arrayRows[i]);
}
return rows}

createArrayRow(JNIEnv * env,int length,jchar rowChar [])函数接受java工作环境,后跟数组长度和要转换为jobjectArray的实际字符数组

static jcharArray createArrayRow(JNIEnv *env, int length, jchar rowChar[]){

//Creating an array of type jcharArray
jcharArray rowOfChars = (*env)->NewCharArray(env, length);

//Setting the region for the array
(*env)->SetCharArrayRegion(env, rowOfChars, 0, length, rowChar);

return rowOfChars;}

get2DArray()函数中的最后一个for循环设置了新创建的jobjectArray行的每个元素,每个jobjectArray都是通过createArrayRow()函数创建的。严格来说,get2DArray()函数返回一个指向jobjectArray的指针,但java会为你解开指针。