java - 我收到一个字符串数组,并希望根据数组中收到的字符串访问哈希图
问题描述
given String arr[] = {"hashmap1", "hashmap2"};
Hashmap<String,Integer> hashmap1;
Hashmap<String,Integer> hashmap2;
我怎样才能做到这一点:
for(int i=0;i<arr.length;i++){
System.out.println(arr[i].get("value"));
// basically, arr[i] is the hashmap name and I want to access it
}
我试图把所有hashmaps
的东西都放进arraylist
去,但那也没有用。
for(int i=0;i<cardNames.length;i++){
boolean tmp = cardNames[i].containsKey(storeType);
我收到此错误:
错误:找不到符号 boolean tmp = cardNames[i].get(storeType); ^ 符号:方法 get(String) 位置:类 String
解决方案
我相信您可以做的就是为您拥有的所有地图创建一个地图,并以名称为键。
像这样的东西:
Map<String, Map<String,Integer>> superHashMap = new HashMap<>();
String arr[] = {"hashmap1", "hashmap2"};
Hashmap<String,Integer> hashmap1;
Hashmap<String,Integer> hashmap2;
superHashMap.put("hashmap1", hashmap1);
superHashMap.put("hashmap2", hashmap2);
How can I do this:
for(int i=0;i<arr.length;i++){
// This shall contain the desired hashMap that you would need for further processing.
Map<String, Integer> mapYouNeedToAccess = superHashMap.get(arr[i]);
}
推荐阅读
- flutter - 如何处理大量图像而不会出现内存不足错误
- c# - NetworkInterface.GetAllNetworkInterfaces() 非常慢
- javascript - 根据 mysql 数据库中的 id 播放音频文件
- html - 当我使用 firebase 部署时,网站的某些部分可以工作,但其他部分不能
- c# - 使用 C# 程序在下载文件之前模拟防病毒检查
- angular - 根据条件更改 mat-autocomplete 选项的值,并过滤这些选项
- apache-spark - spark shuffle write 在小型缓存和操作 df 上爆炸
- python - Matplotlib 中散点图的不同标签
- php - 为什么将集合集合展平为所有数组值的数组?
- python - 错误:命令错误退出状态 1:python setup.py egg_info 检查日志以获取完整命令输出,同时 pip 安装遗传算法