JNI - java ArrayList 转换为 c++ std::string* [英] JNI - java ArrayList conversion to c++ std::string*

查看:28
本文介绍了JNI - java ArrayList 转换为 c++ std::string*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C++ 中的 JNI 进行数据转换.我在使用 javaArrayListstrings 时遇到了麻烦,因为我无法将这样的数据转换为 c++ vectorstd::string*.

I am trying to make a data conversion with JNI in c++. I have come into trouble working with java's ArrayList of strings, since I have not been able to convert such a data into a c++ vector or std::string*.

如果可能的话,我想知道如何在不牺牲太多性能的情况下进行这种转换.任何想法,将不胜感激.

I would like to know how this conversion can be made without sacrificing too much performance, if possible. Any ideas would be appreciated.

推荐答案

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

I don't know if this fits your performance requirements, but it may be a good start.

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

For both options assume that jobject jList; is your ArrayList.

将 List 转换成数组并在数组上迭代(如果你有 LinkedList 而不是 ArrayList 可能更适用)

Convert the List into an array and iterate on the array (maybe more applicable if you have a LinkedList instead of an 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 是一个 RandomAccessList.

Option 2

An alternative would be to use the List.size() and List.get(int) methods to retrieve the data from the list. As you use an ArrayList this solution would also be okay as the ArrayList is an RandomAccessList.

// 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: 将 JNI_FALSE 替换为 NULL_
_edited:用 push_back_ 替换插入

_edited: replaced JNI_FALSE with NULL_
_edited: replaced insert with push_back_

这篇关于JNI - java ArrayList 转换为 c++ std::string*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆