There are 3 by 3 matrices:

[0, 0, 1] [0, 1, 0] [0, 0, 0] [0, 0, 0] [0, 0, 0] [0, 0, 0] ... ... ... 

I need to go through all such matrices and determine the matrices for which the sum of rows / columns / diagonals coincide. Decided to do through reduce:

 List<Object[]> resolvedMatrices = matrices.stream().reduce((a, b) -> { Object temp = new Object(); if(a[0][0] + a[0][1] + a[0][2] == b[0][0] + b[0][1] + b[0][2]) { temp = new Object[]{a, b}; } return temp; }); 

But something kind of garbage I get:

bad return type in lambda expression: Object cannot be converted to int [] [];

What am I doing wrong?

UPD:

Changed data types.

 List<int[][]> resolvedMatrices = matrices.stream().reduce((a, b) -> { int[][] temp = {}; if(a[0][0] + a[0][1] + a[0][2] == b[0][0] + b[0][1] + b[0][2]) { temp = new int[a][b]; // ?????? } return temp; }); 
  • you return the wrong data type - Roman C
  • @RomanC well, this is understandable, but I have never declared int[][] anywhere. - JamesJGoodwin
  • then the if condition should not work - Roman C
  • you would have completely laid out the method, as well as the method from which you call and what you pass on ... it will be much easier to identify the problem - Dmitry
  • @ Dmitriy Aleksandrovich, but why a whole method. All that matters is the variable matrices, which is declared like this: List<int[][]> matrices - JamesJGoodwin

1 answer 1

If you want, without laying out the code, then I can tell you how to write it so that it is simply compiled. will suit you, i have no idea ...

 Optional<int[][]> resolvedMatrices = matrices.stream().reduce((a, b) -> { int[][] temp=null; if (a[0][0] + a[0][1] + a[0][2] == b[0][0] + b[0][1] + b[0][2]) { } return temp; }); 

implement the code in the conditional block at your discretion

better write this way

 private boolean arrayCalc (int[][] array){ int sumArray [] = new int [array.length*2]; boolean result = true; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { sumArray [i]+=array[i][j]; sumArray [array.length+j]+=array[i][j]; } } for (int i = 1; i < sumArray.length; i++) { if (sumArray[i]!=sumArray[i-1]) { result = false; break; } } return result; } 
  • That's exactly the problem with me. Please see the updated question. My conditional statement body in List<int[][]> resolvedMatrices should push a value like [[a], [b]] . But how to do that without changing the type to Object[]{} ? - JamesJGoodwin
  • those. Do you want to return 2 arrays in the return value, or what? - Dmitry
  • Yes exactly. - JamesJGoodwin
  • Well, then an indiscreet question ... And who advised you to do this through reduce? The idea of ​​reduce is to convert all the elements of a stream into a single object. For example, calculate the sum of all elements, or find the minimum element. And you need more than one object. You need a collection consisting of arrays, the sum of the values ​​of which is repeated. Do you want to use streams for this task? I would look in the direction of collect. One of the most powerful pieces in streamAPI, if you wish, you can write your own collector by implementing the Collector interface. - Dmitriy
  • I do not quite understand your goal. You take each element of the collection, there is an array, in which you need to calculate the sum over all rows / columns / diagonals. If in the end all the values ​​match, then add an array to the new collection? or do you still want to compare different arrays to each other? - Dmitriy