2017-04-19 17 views
-1

C言語の関数から2次元文字配列の5x5グリッドをJavaで表示しようとしています。現在使用しているコードは正しい5x5グリッドを返しますが、グリッド内のすべての文字はヌル記号またはランダム記号として表示されます。私が構築するために使用していたコード配列を返すしているが、以下の通りであるC関数からJava JNIへの2D char配列の返却

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'がコメントのように配列の場合、' return 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 [])関数は、配列の長さと、あなたがjobjectArray

に変換したい文字の実際の配列に続いて、javaの作業環境を受け入れます
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ループは、createArrayRow()関数によって作成された各jobjectArrayを使用して、新しく作成されたjobjectArrayの各要素を設定します。厳密に言えば、get2DArray()関数はjobjectArrayへのポインタを返しますが、javaはそのポインタをアンラップします。

関連する問題