我在java文件中有一个本地方法:-
import java.util.Random;
public class QuestionTwoAssignmentOne2018 {
private static final int NUM = 10;
public static void main(String[] args) {
Random bytes = new Random(); // take this out of the loops as mentioned
// in the comments
int count = 100; // keep track of 100 '*'s
int arr[] = new int[100];
for (int a = 0; a < 100; a++) {
arr[a] = bytes.nextInt(10); //
}
for (int i = 0; i < NUM; i++)
{
int bcount = 0;
for (int a = 0; a < 100; a++) {
if (arr[a] == NUM - i)
bcount++;
}
System.out.print(NUM - i + " |");
for (int c = 0; c < bcount; c++)
System.out.print("*");
System.out.println();
}
System.out.print(" "); // for better view
for (int i = 0; i < (NUM * 2) - 1 ; i++)
System.out.print("-");
System.out.println();
System.out.print(" "); // for better view
for (int i = 0; i < NUM; i++)
System.out.print(i + " ");
}
从Java生成头文件后,地图将通过头文件方法转换为jobject:-
class JNITest{
public native void test(String param1, Map<String, Number> param2, Map<String, Map<String, Double>> param3)
}
我在cpp中有一个本地方法:
JNIEXPORT void JNICALL Java_com_jni_JNITest_test
(JNIEnv *env,
jobject self,
jstring param1,
jobject param2,
jobject param3) { }
问:我需要将Jobject转换回std :: map(cpp)才能将其传递给cpp本机方法,请问有人建议使用标准方法进行相同操作吗?
预先感谢。
答案 0 :(得分:1)
我们在C ++ / Java集成方面做了很多工作。跨边界传递复杂的数据结构的问题是,您必须整理方法调用,这可能是非常复杂且容易出错的工作。我发现做这样的事情要容易得多:
我对C ++方面并不熟悉,但是我发现类似的问题正在解决here
答案 1 :(得分:1)
您可以使用scapix::link::java C ++ JNI库在许多C ++和Java类型之间自动转换。这是example:
#include <scapix/java_api/java/lang/System.h>
#include <scapix/java_api/java/util/Locale.h>
#include <scapix/java_api/java/text/DateFormatSymbols.h>
using namespace scapix::link::java;
using namespace scapix::java_api;
void test1()
{
// C++ objects are automatically converted to and from corresponding Java types.
// This works for any type supported by scapix::link::java::convert() interface,
// which supports many STL types and can be extended for your own types.
std::string version = java::lang::System::getProperty("java.version");
std::vector<std::string> languages = java::util::Locale::getISOLanguages();
std::vector<std::vector<std::string>> zone_strings = java::text::DateFormatSymbols::getInstance()->getZoneStrings();
std::map<std::string, std::string> properties = java::lang::System::getProperties();
}
答案 2 :(得分:0)
这需要一些努力。在这里看看:
另外,在这里查看与将Map传递到本地代码有关的示例
http://jnicookbook.owsiak.org/recipe-No-020/ https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo037