【发布时间】:2012-03-07 19:10:06
【问题描述】:
我正在创建 Dictionary 和这样的 ArrayList。
Dictionary testDict, testDict2 = null;
ArrayList al = new ArrayList();
testDict.put ("key1", dataVar1);
testDict.put ("key2", dataVar2);
testDict2.put ("key1", dataVar1);
testDict2.put ("key2", dataVar2);
al.add(testDict);
al.add(testDict2);
现在我的问题是,如何访问字典中的数据?例如,我将如何使用 al 从 testDict 中检索 key1?
提前非常感谢:)
【问题讨论】:
-
我不确定我是否得到了这个问题;您将获得适当的列表条目并像往常一样访问字典。或者遍历列表,检查每个字典的键。
-
我不确定您要做什么,但来自字典 javadocs:
NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.。我认为也许您应该改用 HashMaps,如果您想使用相同的键检索多个值,您可能可以使用带有 putAll 方法的新 HashMap ...只是猜测 -
对不起,我知道这听起来像是一个真正的初学者类型的问题。我正在开发一个棋盘类型的游戏,我们在层之上有层。每一层都是一个字典,而板子本身就是数组列表。没有直接的方法可以立即从我知道的数组列表中访问数据。我希望这能回答一些问题。
-
感谢 jambriz,然后研究 HashMaps。
标签: java dictionary arraylist