JNI开发-Java从C/C++获取List集合对象

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

 

转载于:https://www.cnblogs.com/Free-Thinker/p/10608034.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值