Reputation: 169
I have a 2D ArrayList (ArrayList within an ArrayList). For instance, I have the following values for it:
[res, 0.0]
[print, string]
Now, how can I access the index where the value "res" occurred?
Upvotes: 1
Views: 9606
Reputation: 48258
Iterate over the list in the list:
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
}
}
or use a java8 stream
example:
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);
Upvotes: 2
Reputation: 4592
If list
is your list, you should be able to find the value "res" with:
list.get(0).get(0)
For a reference a[row][col]
to an element of a 2d array, the equivalent reference to an ArrayList
of ArrayList
s (or really any List
of List
s) will be list.get(row).get(col)
Upvotes: 4