2D ArrayList Java中元素的索引

时间:2017-05-14 09:01:00

标签: java arrays list arraylist 2d

我有一个2D ArrayList(ArrayList中的ArrayList)。例如,我有以下值:

[res, 0.0]
[print, string]

现在,我如何访问值" res "发生了?

2 个答案:

答案 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)