JNI - Java的ArrayList的转换为C ++的std ::字符串* [英] JNI - java ArrayList conversion to c++ std::string*

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

问题描述

我试图在用c JNI数据转换++。我来了麻烦与合作的的Java 的的的的ArrayList 字符串的,因为我一直没能到这样的数据转换成的 C ++ 的vector 的std ::字符串*

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*.

我想知道这是如何转换可以在不牺牲太多性能提出,如果可能的话。任何想法将AP preciated。

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.

转换列表到一个阵列和阵列上迭代(也许更适用,如果你有一个链表,而不是一个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, JNI_FALSE);
    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, JNI_FALSE);
    sVector.insert(sVector.end(), chr);
    env->ReleaseStringUTFChars(strObj, chr);
}

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

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

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