I am creating Dictionary and an ArrayList such as this.
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);
now my issue here is, how can I access the data within the dictionaries? Like for example how would I retrieve key1 from testDict using al?
Many thanks in advance 🙂
As you can read in the Java Docs all Dictionary objects (note that e.g. Hashtable is one of them) have a method
Object get(Object key)to access it’s elements. In your example you could access the value of the entrykey1intextDictlike that:Note, that you nowhere initialize your Dictionary objects and that the
Dictionaryclass is abstract. So you could for example use aHashtable(or if you don’t need synchronized access use a fasterHashMapinstead) for your purpose like this:And make sure to use the correct generic types (the second one has to be the type that your
dataVars have)