我有一个二维ArrayList(ArrayList中的ArrayList)。例如,我具有以下值:
[res, 0.0]
[print, string]
现在,如何访问出现“ res”值的索引?
最佳答案
遍历列表中的列表:
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);
关于java - 2D ArrayList Java中元素的索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43962132/