Java中JNI的使用详解第五篇:C/C++中操作Java中的数组(二)

2014-11-24 07:25:48 · 作者: · 浏览: 1
  • cout<<"数组排序后的结果:";
  • for(jsize i=0;i cout< }
  • cout< //释放数组指针
  • env->ReleaseIntArrayElements(jint_arr,int_arr,JNI_ABORT);
  • //获取Java中对象Father数组属性的id
  • jfieldID fid_obj_arrays = env->GetFieldID(env->GetObjectClass(obj),"objArrays","[Lcom/jni/demo/Father;");
  • //获取Java中对象数组Father属性objArrays的对象
  • jobjectArray jobj_arr = (jobjectArray)env->GetObjectField(obj,fid_obj_arrays);
  • //从对象数组中获取索引值为1的对象Father
  • jobject jobj = env->GetObjectArrayElement(jobj_arr,1);
  • //获取Father对象的class对象
  • jclass clazz_father = env->GetObjectClass(jobj);
  • //获取Father对象中的function方法的id
  • jmethodID id_father_function = env->GetMethodID(clazz_father,"function","()V");
  • //调用Father对象中的function方法
  • env->CallVoidMethod(jobj,id_father_function);
  • //在本地创建一个大小为10的对象数组,对象的初始化都是jobj,也就是方法的第三个参数
  • jobjectArray jobj_arr_temp = env->NewObjectArray(10,env->GetObjectClass(jobj),jobj);
  • //获取本地对象数组中第4个对象
  • jobject jobj_temp = env->GetObjectArrayElement(jobj_arr_temp,3);
  • //调用Father对象中的function方法
  • env->CallVoidMethod(jobj_temp,id_father_function);
  • }

  • 在Eclipse编译运行结果如下:

    不要以为这就结束了,后面还有很多内容呀!