java - How to access String within an ArrayList within an ArrayList within a HashMap? -
so have arraylist full of strings
arraylist<string> colours = new arraylist<>(); colours.add("red"); colours.add("blue");
and arraylist stored in arraylist
arraylist<arraylist> container = new arraylist<>(); container.add(colors);
and arraylist stored in hashmap
hashmap<integer, arraylist> map = new hashmap<>(); map.put(1, container);
how access "red"? tried
system.out.println(map.get(1).get(0).get(0));
but gave a
error: java: cannot find symbol symbol: method get(int) location: class java.lang.object
you should not use raw types arraylist<arraylist>
use "cooked" types such arraylist<arraylist<string>>
(or better, list<list<string>>
).
likewise, instead of hashmap<integer, arraylist>
, use hashmap<integer, arraylist<arraylist<string>>>
(or better, map<integer, list<list<string>>>
).
if make these changes, map.get(1).get(0).get(0)
expression compile correctly.
Comments
Post a Comment