NI开发有时需要Java从C/C++获取List对象,此篇主要讲解Java从C/C++获取List<Student>返回值; 1. 定义com.niubashaoye.simple.jni.StuInfo类 public class StuInfo { private int stuId; private String stuName; private int stuAge; private String className; public StuInfo(int stuId, String stuName, int stuAge, String className) { super(); this.stuId = stuId; this.stuName = stuName; this.stuAge = stuAge; this.className = className; } //getter() //setter() @Override public String toString() { return "StuInfo [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + ", className=" + className + "]"; } } 2. 添加native函数 public class JNITools { static { System.loadLibrary("TestDemo"); } /** * 从C获取list * * @return */ public native List<StuInfo> getStuList(); } * 从C获取list * * @return */ public native List<StuInfo> getStuList(); } 3. C/C++文件 3.1 添加StuInfo结构体 typedef struct { int stuId; char stuName[50]; int stuAge; char className[50]; } StuInfo; 3.2 获取List<StuInfo>对象 JNIEXPORT jobject JNICALL Java_com_niubashaoye_simple_jni_JNITools_getStuList( JNIEnv *env, jobject obj) { //获取ArrayList类引用 jclass list_jcs = env->FindClass("java/util/ArrayList"); if (list_jcs == NULL) { LOGI("ArrayList no find!"); return NULL; } //获取ArrayList构造函数id jmethodID list_init = env->GetMethodID(list_jcs, "<init>", "()V"); //创建一个ArrayList对象 jobject list_obj = env->NewObject(list_jcs, list_init, ""); //获取ArrayList对象的add()的methodID jmethodID list_add = env->GetMethodID(list_jcs, "add", "(Ljava/lang/Object;)Z"); //获取StuInfo类 jclass stu_cls = env->FindClass("com/niubashaoye/simple/jni/StuInfo"); //获取StuInfo的构造函数 jmethodID stu_init = env->GetMethodID(stu_cls, "<init>", "(ILjava/lang/String;ILjava/lang/String;)V"); StuInfo stuInfo; stuInfo.stuId = 100001; stuInfo.stuAge = 20; const char *name = "牛八少爷"; strcpy(stuInfo.stuName, name); const char *className = "高三六班"; strcpy(stuInfo.className, className); for (int i = 0; i < 4; i++) { LOGI("第%d个学生信息:id=%d,name=%s,age=%d,className=%s", i + 1, (stuInfo.stuId + i), stuInfo.stuName, (stuInfo.stuAge + i), stuInfo.className); //通过StuInfo的构造函数创建StuInfo对象 jobject stu_obj = env->NewObject(stu_cls, stu_init, (stuInfo.stuId + i), env->NewStringUTF(stuInfo.stuName), (stuInfo.stuAge + i * 5), env->NewStringUTF(stuInfo.className)); env->CallBooleanMethod(list_obj, list_add, stu_obj); } return list_obj; } JNIEnv *env, jobject obj) { //获取ArrayList类引用 jclass list_jcs = env->FindClass("java/util/ArrayList"); if (list_jcs == NULL) { LOGI("ArrayList no find!"); return NULL; } //获取ArrayList构造函数id jmethodID list_init = env->GetMethodID(list_jcs, "<init>", "()V"); //创建一个ArrayList对象 jobject list_obj = env->NewObject(list_jcs, list_init, ""); //获取ArrayList对象的add()的methodID jmethodID list_add = env->GetMethodID(list_jcs, "add", "(Ljava/lang/Object;)Z"); //获取StuInfo类 jclass stu_cls = env->FindClass("com/niubashaoye/simple/jni/StuInfo"); //获取StuInfo的构造函数 jmethodID stu_init = env->GetMethodID(stu_cls, "<init>", "(ILjava/lang/String;ILjava/lang/String;)V"); StuInfo stuInfo; stuInfo.stuId = 100001; stuInfo.stuAge = 20; const char *name = "牛八少爷"; strcpy(stuInfo.stuName, name); const char *className = "高三六班"; strcpy(stuInfo.className, className); for (int i = 0; i < 4; i++) { LOGI("第%d个学生信息:id=%d,name=%s,age=%d,className=%s", i + 1, (stuInfo.stuId + i), stuInfo.stuName, (stuInfo.stuAge + i), stuInfo.className); //通过StuInfo的构造函数创建StuInfo对象 jobject stu_obj = env->NewObject(stu_cls, stu_init, (stuInfo.stuId + i), env->NewStringUTF(stuInfo.stuName), (stuInfo.stuAge + i * 5), env->NewStringUTF(stuInfo.className)); env->CallBooleanMethod(list_obj, list_add, stu_obj); } return list_obj; } 4. Java调用native函数 List<StuInfo> stuList = jniTools.getStuList(); Toast.makeText(getApplicationContext(), stuList.toString(), Toast.LENGTH_SHORT).show(); List<StuInfo> stuList = jniTools.getStuList(); Toast.makeText(getApplicationContext(), stuList.toString(), Toast.LENGTH_SHORT).show(); 代码下载:https://download.csdn.net/download/niuba123456/10533712