2017-04-19 130 views
-1

我想从java函数中的C函数显示来自二维char数组的5x5字符网格。我目前使用的代码返回一个正确的5x5网格,但网格中的所有字符都显示为空或随机符号。我使用以构建被返回数组的代码如下:从C函数返回二维字符数组到Java JNI

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; } 

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

+0

在C中,如果'rows'是一个数组,就像注释所说的那样,'returns rows;'只返回一个指针而不是一个数组,返回一个现在无效的对象。建议使用完整的最小代码示例来复制此问题, – chux

回答

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长度,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()函数中的last for循环设置新创建的jobjectArray行的每个元素,并通过createArrayRow()函数创建每个jobjectArray。严格地说,get2DArray()函数返回一个指向jobjectArray的指针,但是java会为你解开指针。