你总是可以直接使用 JNI :
假设你想检索一个 xyz 向量数组
public class demo{
public native vecteur[] returnArray();
....
}
在 swig wrap cxx 文件中添加将填充 java vect 数组的函数
JNIEXPORT jobjectArray JNICALL
Java_demo_returnArray
(JNIEnv *env, jobject jobj){
jobjectArray ret;
int i;
jclass objClass;
jmethodID mid;
jobject myobj;
jmethodID setX;
objClass = env->FindClass("vect");
if (!objClass)
{
printf("class not found\n");
exit(0);
}
ret= (jobjectArray)env->NewObjectArray(5, // change with the size of your array or a variable
objClass,
env->NewStringUTF(""));//FIXME
// call javap -s myclass to know the names
mid=env->GetMethodID( objClass, "<init>", "()V"); // looking for the vect class constructor
if (!mid)
{
printf("vect() not found\n");
exit(0);
}
for(i=0;i<5;i++) {
myobj=env->NewObject( objClass, mid); // myobj = new vect()
// get vect::setX method
setX=env->GetMethodID( objClass, "setX", "(F)V"); // looking for vect::setX method
if(!setX)
{
printf("method vect::setX not found\n");
exit(0);
}
// call setX method with param i
env->CallVoidMethod(myobj, setX,(float)i); // change i with your array value
env->SetObjectArrayElement(
ret,i,myobj);
}
return(ret);
}
最后是主要课程
// main.java
public class main {
static {
System.loadLibrary("myclass");
}
public static void main(String argv[]) {
demo l = new demo();
vecteur f[] = l.returnArray();
System.out.println("array size : "+f.length);
for (int i = 0;i < f.length;i++)
System.out.println(f[i].getX());
}
}