所以我有这个充满字符串的 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)
表达式将正确编译。
关于java - 如何在 HashMap 中的 ArrayList 中访问 ArrayList 中的 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35262144/