我有一个2D ArrayList(ArrayList中的ArrayList)。例如,我有以下值:
[res, 0.0]
[print, string]
现在,我如何访问值" res "发生了?
答案 0 :(得分:1)
迭代列表中的列表:
List<List<String>> dList = new ArrayList<>();
dList.add(Arrays.asList("A", "B", "C"));
dList.add(Arrays.asList("A", "B", "C"));
dList.add(Arrays.asList("A", "B", "C"));
for (List<String> list : dList) {
if (list.contains("A")) {
// todo
}
}
或使用java8流
示例:
List<List<String>> dList = new ArrayList<>();
dList.add(Arrays.asList("A", "B", "C"));
dList.add(Arrays.asList("f", "t", "j"));
dList.add(Arrays.asList("g", "4", "h"));
String a = dList.stream().flatMap(List::stream).filter(xx -> xx.equals("a")).findAny().orElse(null);
a = dList.stream().flatMap(List::stream).filter(xx -> xx.equalsIgnoreCase("a")).findFirst().orElse(null);
a = dList.stream().flatMap(List::stream).filter(xx -> xx.equals("h")).findFirst().orElse(null);
答案 1 :(得分:0)
如果list
是您的列表,您应该能够找到值&#34; res&#34;用:
list.get(0).get(0)
对于2d数组的元素的引用a[row][col]
,对ArrayList
ArrayList
的{{1}}(或List
List
的等效引用} s)将是list.get(row).get(col)