簡體   English   中英

如何在java中映射[u] .push_back(v)(c ++)?

[英]How to do map[u].push_back(v) (c++) in java?

在C ++中如果我像這樣聲明map STL map< int ,vector < int > > m; 我可以在m[u].push_back(v); 以這種方式。 但是我怎么能用Java做到這一點?

如果我理解你的問題,這應該是你正在尋找的。

Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>();
map.put(0, new ArrayList<Integer>());
Integer someInt = 1;
map.get(0).add(someInt); //get arraylist and add to back.

像這樣的東西,

public static void main(String[] args) {
    int u = 0;
    int v = 1;
    Map<Integer, ArrayList<Integer>> map = 
        new HashMap<Integer, ArrayList<Integer>>();
    if (map.get(u) == null) { // Add a List if they key is null.
        map.put(u, new ArrayList<Integer>());
    }
    // Add v to the List at u.
    map.get(u).add(v);
    System.out.println(map);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM