2017-04-19 129 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會爲你解開指針。