久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何通過 JNI 將 HashMap 從 Java 發(fā)送到 C

How to send a HashMap from Java to C via JNI(如何通過 JNI 將 HashMap 從 Java 發(fā)送到 C)
本文介紹了如何通過 JNI 將 HashMap 從 Java 發(fā)送到 C的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 Object 有一個 HashMap 字段.當 Object 傳遞給 C 時,如何訪問該字段?

I have an Object that has a HashMap field. When the Object is passed to C, how can I access the field?

ObjectClass有以下字段:

private String hello;
private Map<String, String> params = new HashMap<String, String>();

推薦答案

你的問題的答案真的歸結為為什么你想將 Map 傳遞給C 而不是在 Java 中迭代您的 Map 并將內容傳遞給 C.但是,我該質疑為什么?

The answer to your question really boils down to why you'd want to pass a Map to C rather than iterate your Map in Java and pass the contents to C. But, who am I to question why?

您問如何訪問 HashMap(在您提供的代碼中,Map)字段?在 Java 中為其編寫訪問器方法,并在傳遞容器 Object 時從 C 中調用該訪問器方法.下面是一些簡單的示例代碼,展示了如何將 Map 從 Java 傳遞到 C,以及如何訪問 Mapsize() 方法代碼>.從中,您應該能夠推斷出如何調用其他方法.

You ask how to access the HashMap (in your provided code, Map) field? Write an accessor method for it in Java and call that accessor method from C when you pass the container Object. Below is some bare-bones sample code showing how to pass a Map from Java to C, and how to access the size() method of the Map. From it, you should be able to extrapolate how to call other methods.

容器對象:

public class Container {

    private String hello;
    private Map<String, String> parameterMap = new HashMap<String, String>();

    public Map<String, String> getParameterMap() {
        return parameterMap;
    }
}

將容器傳遞給 JNI 的主類:

Master Class which passes a Container to JNI:

public class MyClazz {

    public doProcess() {

        Container container = new Container();
        container.getParameterMap().put("foo","bar");

        manipulateMap(container);
    }

    public native void manipulateMap(Container container);
}

相關C函數(shù):

JNIEXPORT jint JNICALL Java_MyClazz_manipulateMap(JNIEnv *env, jobject selfReference, jobject jContainer) {

    // initialize the Container class
    jclass c_Container = (*env)->GetObjectClass(env, jContainer);

    // initialize the Get Parameter Map method of the Container class
    jmethodID m_GetParameterMap = (*env)->GetMethodID(env, c_Container, "getParameterMap", "()Ljava/util/Map;");

    // call said method to store the parameter map in jParameterMap
    jobject jParameterMap =  (*env)->CallObjectMethod(env, jContainer, m_GetParameterMap);

    // initialize the Map interface
    jclass c_Map = env->FindClass("java/util/Map");

    // initialize the Get Size method of Map
    jmethodID m_GetSize = (*env)->GetMethodID(env, c_Map, "size", "()I");

    // Get the Size and store it in jSize; the value of jSize should be 1
    int jSize = (*env)->CallIntMethod(env, jParameterMap, m_GetSize);

    // define other methods you need here.
}

值得注意的是,我并不熱衷于在方法本身中初始化 methodID 和類;this SO Answer 向您展示了如何緩存它們以供重復使用.

Of note, I'm not crazy about initializing methodIDs and classes in the method itself; this SO Answer shows you how to cache them for re-use.

這篇關于如何通過 JNI 將 HashMap 從 Java 發(fā)送到 C的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創(chuàng)建一個HashMap.出發(fā)點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 久久男人 | 国产黄色大片在线免费观看 | 婷婷综合网| 久久精品一区 | 成人a视频| 日本一区二区不卡视频 | 久久影院一区 | 欧美午夜一区 | 国产精品一区在线观看你懂的 | 久久久在线视频 | 日韩免费高清视频 | 欧美黑人国产人伦爽爽爽 | 亚洲综合色视频在线观看 | 精品国产一区二区国模嫣然 | 日韩第一页 | 天天人人精品 | 精品国产一区二区久久 | 中文字幕国产第一页 | 日韩中出 | 一区二区av | 日韩av一区二区在线观看 | 日韩精品久久久久 | 亚洲精品不卡 | 污污免费网站 | 日本视频免费 | 人人人人干 | 久草在线 | 欧美中文视频 | 国产精品精品视频一区二区三区 | 一区二区三区四区不卡 | 国产91一区| 99精品一区二区三区 | 亚洲国产欧美国产综合一区 | 国产亚洲人成a在线v网站 | 国产精品国产亚洲精品看不卡15 | 国内av在线 | 99精品久久 | 精精国产视频 | 精品欧美一区二区三区久久久 | 成人在线视频观看 | 午夜成人免费视频 |