所以我有这个充满字符串的ArrayList
ArrayList<String> colours = new ArrayList<>();
colours.add("Red");
colours.add("Blue");
那个ArrayList被存储在另一个ArrayList中
ArrayList<ArrayList> container = new ArrayList<>();
container.add(colors);
并将ArrayList存储在HashMap中
HashMap<Integer, ArrayList> map = new HashMap<>();
map.put(1, container);
如何访问“红色”?我试过了
System.out.println(map.get(1).get(0).get(0));
但是它给了
Error: java: cannot find symbol
symbol: method get(int)
location: class java.lang.Object
最佳答案
您不应使用诸如ArrayList<ArrayList>
之类的原始类型,而应使用诸如ArrayList<ArrayList<String>>
(甚至更好的是List<List<String>>
)之类的完全“熟化”的类型。
同样,代替HashMap<Integer, ArrayList>
,使用HashMap<Integer, ArrayList<ArrayList<String>>>
(甚至更好的是Map<Integer, List<List<String>>>
)。
如果进行这些更改,您的map.get(1).get(0).get(0)
表达式将正确编译。