JNI-java ArrayList转换为c++的std::string*。

 2023-01-20    261  

问题描述

我试图在C ++中使用JNI进行数据转换.我与 strings strings 的 java ‘s arraylist 一起遇到困难,因为我无法将此类数据转换为a c ++ vector 或 std :: string*.

我想知道如何在不牺牲过多绩效的情况下进行这种转换.任何想法,将不胜感激.

JNI-java ArrayList转换为c++的std::string*。

推荐答案

我不知道这是否适合您的性能要求,但这可能是一个很好的开始.

对于两个选项,假设jobject jList;是您的arraylist.

选项1

将列表转换为数组并在数组上迭代(如果您有LinkedList而不是ArrayList,则可能更适用)

)

// retrieve the java.util.List interface class
jclass cList = env->FindClass("java/util/List");

// retrieve the toArray method and invoke it
jmethodID mToArray = env->GetMethodID(cList, "toArray", "()[Ljava/lang/Object;");
if(mToArray == NULL)
    return -1;
jobjectArray array = (jobjectArray)env->CallObjectMethod(jList, mToArray);

// now create the string array
std::string* sArray = new std::string[env->GetArrayLength(array)];
for(int i=0;i<env->GetArrayLength(array);i++) {
    // retrieve the chars of the entry strings and assign them to the array!
    jstring strObj = (jstring)env->GetObjectArrayElement(array, i);
    const char * chr = env->GetStringUTFChars(strObj, NULL);
    sArray[i].append(chr);
    env->ReleaseStringUTFChars(strObj, chr);
}

// just print the array to std::cout
for(int i=0;i<env->GetArrayLength(array);i++) {
    std::cout << sArray[i] << std::endl;
}

选项2

一种替代方法是使用List.size()和List.get(int)方法从列表中检索数据.当您使用arraylist时,此解决方案也可以,因为arraylist是一个随机列表列表.

// retrieve the java.util.List interface class
jclass cList = env->FindClass("java/util/List");

// retrieve the size and the get method
jmethodID mSize = env->GetMethodID(cList, "size", "()I");
jmethodID mGet = env->GetMethodID(cList, "get", "(I)Ljava/lang/Object;");

if(mSize == NULL || mGet == NULL)
    return -1;

// get the size of the list
jint size = env->CallIntMethod(jList, mSize);
std::vector<std::string> sVector;

// walk through and fill the vector
for(jint i=0;i<size;i++) {
    jstring strObj = (jstring)env->CallObjectMethod(jList, mGet, i);
    const char * chr = env->GetStringUTFChars(strObj, NULL);
    sVector.push_back(chr);
    env->ReleaseStringUTFChars(strObj, chr);
}

// print the vector
for(int i=0;i<sVector.size();i++) {
    std::cout << sVector[i] << std::endl;
}

_edited:用null_
替换jni_false
_edited:用push_back _

替换插入

其他推荐答案

您也可以使用JNI内的经典Java迭代器,使循环同时优化了ArrayList和LinkedList.

.

jobject jIterator = env->CallObjectMethod(jList, env->GetMethodID(env->GetObjectClass(jList), "iterator", "()Ljava/util/Iterator;"));
jmethodID nextMid = env->GetMethodID(env->GetObjectClass(jIterator), "next", "()Ljava/lang/Object;");
jmethodID hasNextMid = env->GetMethodID(env->GetObjectClass(jIterator), "hasNext", "()Z");

while (env->CallBooleanMethod(jIterator, hasNextMid)) {
    jobject jItem = env->CallObjectMethod(jIterator, nextMid);
    /* do something with jItem */
}

以上所述是小编给大家介绍的JNI-java ArrayList转换为c++的std::string*。,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26083.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。