本文介绍了将字符串的arraylist从Native java返回到JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

           ArrayList<String> myArraylist;

           public ArrayList<String> getData(){
               myArraylist = new ArrayList<String>();
               myArraylist.add("1267982563");
               myArraylist.add("2345678");
               myArraylist.add("5432789");
               return myArraylist;
           }

如何在JNI端获取上述方法中的每个项目并推送到向量并从JNI返回到JNI层中的其他CPP调用。

How to get the each items from the above method in JNI side and Push to vector and return from the JNI to other CPP calls in the JNI layer.

推荐答案

向量重建列表,您将展开上述方法:

If you choose to reconstruct the list from the vector, you will unwind the above method:

jobject cpp2java(std::vector<std::string> vector) {
  jobject result = env->NewObject(java_util_ArrayList, java_util_ArrayList_, vector.size());
  for (std::string s: vector) {
    jstring element = env->NewStringUTF(s.c_str());
    env->CallBooleanMethod(result, java_util_ArrayList_add, element);
    env->DeleteLocalRef(element);
  }
  return result;
}

无论如何,在使用Jni时要小心线程,始终附加在本机线程被销毁之前,你的本机线程和分离。

At any rate, be careful with threads when you work with Jni, always attach your native threads and detach before the native thread is destroyed.

这篇关于将字符串的arraylist从Native java返回到JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 20:34
查看更多