There was this problem on HackerRank Day 11: 2D Arrays[1](Read the problem statement here) in which can be solved easily using arrays. But they have used 2D lists to store the data. But i could'nt figure out anyways to access contents of multi-dimensional lists by refering their index.
This is the code given to solve:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<List<Integer>> arr = new ArrayList<>();
IntStream.range(0, 6).forEach(i -> {
try {
arr.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
}
}
What are the possible solutions for this problem using Lists.
I tried by using toArray() method and get() method in various ways but failed. Any help would be appreciated. [1]: https://www.hackerrank.com/challenges/30-2d-arrays/problem
int maxval = -6*9;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
//System.out.print(arr.get(i).get(j) + " ");
int sum = arr.get(i).get(j) + arr.get(i).get(j+1) + arr.get(i).get(j+2);
sum+=arr.get(i+1).get(j+1);
sum+=arr.get(i+2).get(j) + arr.get(i+2).get(j+1) + arr.get(i+2).get(j+2);
if(sum > maxval)
maxval = sum;
}
}
System.out.println(maxval);