如何在java中实现map[u].push_back(v) (c++)
How to do map[u].push_back(v) (c++) in java?
在c++中,如果我像map< int ,vector < int > > m;
这样声明map STL,我可以在m[u].push_back(v);
中以这种方式使用它。但是如何在Java中做到这一点呢?
就像这样,
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);
}
如果我理解你的问题,这应该是你正在寻找的。
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.
相关文章:
- 推导 std::vector::back() 的返回类型
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.push_back(vector.back()+1) 是未定义的行为吗?
- C++自己的对象堆栈.在 push() 上复制了什么?
- C++ push() 和 pop() 方法使用指针的动态 LinkedList 的问题
- 如何像在javascript中一样"push" c ++映射
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- std::vector using back(), pop_back(), push_back(), 得到'double free or corruption'错误
- vector.back()和vector.end()有什么区别
- 列表大小为 1,但 front() 和 back() 不相等
- 返回C++中没有 back() 方法的容器的最后一个元素?
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 模板堆栈类的复制构造函数中的 pop() 和 push() 不起作用
- 我们可以使用Back() - 值索引在其上执行向量插入
- 当我运行以下代码添加str.front() str.back时,它给了我200个,但为什么
- push(ing)back在循环中对象指针
- 在 C++ 中将元素添加到空向量:为什么 push.back 有效而 [] 无效
- 我似乎无法让 v.push.back() 处理字符串
- String::push_back() does not push back
- 向量resize(), push.back(), reserve()方法