Return a String array on a JNI method

后端 未结 2 988
[愿得一人]
[愿得一人] 2020-12-13 07:43

I need to get a List of Strings (char*) from C++ and return it to Java.

How can I do that?

I think one solution is return a big string pre-defined like: \"[i

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 07:57

    ret= (jobjectArray)env->NewObjectArray(5,env->FindClass("java/lang/String"),env->NewStringUTF(""));
    

    I think the initial element initialized to "" (empty string)

    env->NewStringUTF("")
    

    is not needed, as you assign a new value to the array element just after:

    for(i=0;i<5;i++) env->SetObjectArrayElement(ret,i,env->NewStringUTF(data[i]));
    

    A simple "NULL" would be enough in this case, as the initial element specified will be electable for garbage collection as soon as the next line is executed. It's like writing the following in Java code:

    int i = 0;
    i = 1;
    

    Or worse:

    Object object = new BigObjectVeryHeavyToInitialize();
    object = new AnotherObject();
    

    Your favorite IDE would issue you a little warning for that.

提交回复
热议问题