gpt4 book ai didi

java - 将 2D 字符数组从 C 函数返回到 java JNI

转载 作者:行者123 更新时间:2023-11-30 19:34:44 24 4
gpt4 key购买 nike

我正在尝试在 java 中显示来自 C 函数的 2D 字符数组的 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 字符数组。人物。 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; }

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

最佳答案

我采用的解决方案创建一个字符数组,然后将每个数组合并在一起以创建字符数组的 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 的每个元素、行,以及通过 createArrayRow() 函数创建的每个 jobjectArray。严格来说, get2DArray() 函数返回一个指向 jobjectArray 的指针,但 java 会为您解开该指针。

关于java - 将 2D 字符数组从 C 函数返回到 java JNI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43491757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com