Android 从嵌套HashMap检索数据
这是我的HashMap声明:Android 从嵌套HashMap检索数据,android,hashmap,Android,Hashmap,这是我的HashMap声明: HashMap<String, HashMap<ArrayList<String>, ArrayList<String>>> myMap; HashMap-myMap; 现在我已经创建了2个arraylist ArrayList<String> FirstAL = new ArrayList<String>(); ArrayList<String> SecondAL = new
HashMap<String, HashMap<ArrayList<String>, ArrayList<String>>> myMap;
HashMap-myMap;
现在我已经创建了2个arraylist
ArrayList<String> FirstAL = new ArrayList<String>();
ArrayList<String> SecondAL = new ArrayList<String>();
ArrayList FirstAL=new ArrayList();
ArrayList SecondAL=新的ArrayList();
在onCreate()中,我向arraylist和HashMap添加了项
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirstAL.add("a");
FirstAL.add("b");
FirstAL.add("c");
SecondAL.add("1");
SecondAL.add("2");
SecondAL.add("3");
myMap.put("myKey", new HashMap<ArrayList<String>, ArrayList<ArrayList<String>>>());
myMap.get("myKey").put(FirstAL, SecondAL);
// now while retriving I'm getting null instead of Arraylist
ArrayList<String> resultAL = myMap.get("myKey").get("a");
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
第一部分添加(“a”);
第一部分添加(“b”);
第一部分添加(“c”);
第二,添加(“1”);
第二,添加(“2”);
第二,添加(“3”);
put(“myKey”,newhashmap());
myMap.get(“myKey”).put(FirstAL,SecondAL);
//现在在重试时,我得到的是null而不是Arraylist
arraylistresultal=myMap.get(“myKey”).get(“a”);
}
提前通知我。请帮我找出哪里出了问题?您使用:
myMap.get("myKey").put(FirstAL, SecondAL);
其中,FirstAL
是ArrayList
,但是您尝试使用字符串
键检索存储的值:
myMap.get("myKey").get("a");
使用
FirstAL
而不是“a”
或将myMap
声明为MapUseFirstAL
作为键检索值作为myMap.get(“myKey”).get(FirstAL)代码>我正在尝试你的建议,瞧,它起作用了。它给出了SecondAL作为结果,但是如何确定FirstAL的哪个键?是的,用FirstAL替换“a”就可以了,但是你能告诉我返回的结果是哪个FirstAL键吗?如何指定键“a”来检索结果ArrayList?@kevz您可以使用该方法,检查示例假设a、b和c键的值不同。那么,如何检索与a、b和c相关的值?我不知道你想说的是什么。indexOf@kevz那你一定要用Mapallright去查。我设计错了。谢谢你的指导。