我在java中创建了一个整数数组并通过jni将数组传递给cpp程序我的代码是:
import java.util.*;
class SendArray {
//Native method declaration
native int[] loadFile(int[] name);
//Load the library
static {
System.loadLibrary("nativelib");
}
public static void main(String args[]) {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
//Create class instance
SendArray mappedFile=new SendArray();
//Call native method to load SendArray.java
int[] buf = mappedFile.loadFile(arr);
//Print contents of SendArray.java
for(int i=0;i<buf.length;i++) {
System.out.print(buf[i]);
}
}
}
在 cpp 程序中,我正在反转数组并将数组返回给 java programee 我的代码是::
#include <iostream>
using namespace std;
JNIEXPORT jintArray JNICALL Java_SendArray_loadFile
(JNIEnv * env, jobject jobj, jintArray array) {
cout<<"Orignal Array is:"<<endl;
int i;
jboolean j;
int ar[100];
// for(i = 0; i < 10; i++){
int * p= env->GetIntArrayElements(array, &j);
//jint *array=env->GetIntArrayElements(one, 0);
//ar[i] = array[i];
//}
for(i = 0 ; i < 10 ; i++){
cout << p[i];
}
for(i = 10 ; i > 0 ; i--){
ar[10-i] = p[i];
}
jintArray ret = env->NewIntArray(10);
for(i = 0; i >10 ; i++){
ret[i]=ar[i];
}
return ret;
}
我刚开始的错误是:
error: no match for 'operator=' in '*(ret +((long unsigned int)((long unsigned int)i))) = ar[i]'
我应该怎么做才能将数组返回给java程序????请帮忙!!!!!