I have created a 2 dimensional integer array for example
int[][] source = new int[][]{
new int[]{1, 2, 3, 4},
new int[]{-3, -7, -5, -5},
new int[]{11, 3, 7, 9},
new int[]{121, -41, 515, -466}
};
now I am creating an array of maximum value across the rows as :
int[] rowMax = new int[source.length];
for (int i = 0; i < source.length; i++) {
int m = Integer.MIN_VALUE;
for (int j = 0; j < source[0].length; j++) {
if (source[i][j] > m) {
m = source[i][j];
}
}
rowMax[i] = m;
}
This works fine for me. Giving this a try with streams I could figure out that Finding the max/min value in an array of primitives using Java, in which case
int max = Arrays.stream(arr).max().getAsInt();
and I can update my inner loop as:
int[] rowMax = new int[source.length];
for (int i = 0; i < source.length; i++) {
rowMax[i] = Arrays.stream(source[i]).max().getAsInt();
}
But I am not sure of how to do this for complete int[] rowMax
if possible using streams further. Could this be converted at all?