[英]How to compare a bi-dimensional array
I have this bi-dimensional array: 我有这个二维数组:
int[][] matrix = new int[][] {
{ 4, 4, 4, 4, 4},
{ 4, 3, 4, 3, 4},
{ 4, 3, 4, 3, 4},
{ 4, 3, 4, 3, 3},
{ 1, 1, 1, 3, 3},
{ 4, 4, 4, 3, 3},
{ 4, 5, 4, 3, 3},
{ 4, 4, 4, 3, 3}};
First I want to compare the first element with the second, then the second with the third and so on, row by row. 首先,我想将第一个元素与第二个元素进行比较,然后将第二个元素与第三个元素进行比较,依此类推,逐行比较。
Then I want to do the same thing on a column by column basis, comparing the element [0,0] with the element [1,0], then comparing [1,0] with [2,0] and so on, for each column. 然后,我想逐列执行相同的操作,将元素[0,0]与元素[1,0]比较,然后将[1,0]与[2,0]比较,依此类推,每列。
That is an easy thing to be done using for() loops: 使用for()循环很容易做到:
for (int r=0; r<A.length;r++) {
for (int c=0; c<A[r].length;c++){
if (if (matrix[r][c]==matrix[r][c+1])){
// do somet
}
}
}
But what I need is to do the same using Java 8 functional programming with lambdas and streams iteration. 但是我需要使用带有lambda和流迭代的Java 8函数编程来执行相同的操作。
You question is a bit unclear to provide universally applicable code. 您不清楚提供通用代码的问题。 But probably has enough to set you on the right path. 但是可能足以让您走上正确的道路。
Let's assume you just wanted to find adjacent duplicates: 假设您只是想查找相邻的重复项:
//reference code
for (int[] row : matrix) {
for (int c = 0; c < row.length - 1; c++) {
if (row[c] == row[c + 1]) {
System.out.print(row[c]+" ");
}
}
}
//prints 4 4 4 4 3 1 1 3 4 4 3 3 4 4 3
Let's also assume your matrix will never contain -1
. 我们还假设您的矩阵永远不会包含-1
。 Then you could do 那你可以做
//simple accumulator with a side effect of print out
IntBinaryOperator accumulator = (acc, x) -> {
if (acc == x) System.out.print(x + " ");
return x;
};
//also prints 4 4 4 4 3 1 1 3 4 4 3 3 4 4 3
Arrays.stream(matrix).forEach(arr -> Arrays.stream(arr).reduce(-1, accumulator));
reduce(-1, accumulator)
would work on any int array to find same numbers next to each other. reduce(-1, accumulator)
可以在任何int数组上工作,以查找彼此相邻的相同数字。 Accumulator function keeps previous number in acc and compares with incoming one. 累加器功能将先前的数字保留在acc中,并与传入的数字进行比较。 Of course, identity as -1
is cheating, canonical way would be to use Arrays.stream(arr).boxed()...
and use null
as identity (and it still would be cheating, but will allow all ints in your matrix). 当然,标识为-1
就是作弊,规范的方式是使用Arrays.stream(arr).boxed()...
并使用null
作为标识(它仍然会作弊,但是会允许矩阵中的所有整数)。
Here is another version: 这是另一个版本:
//prints 4 4 4 4 4 4 4 3 1 1 3 4 4 3 3 4 4 3
Arrays.stream(matrix).flatMapToInt(Arrays::stream).reduce(-1, accumulator);
This time no forEach(), but since it flattens the matrix, it will compare last element of a row with first element of the next row - could be a bug or a feature. 这次没有forEach(),但是由于它使矩阵变平,因此它将行的最后一个元素与下一行的第一个元素进行比较-可能是错误或功能。
Finally, accumulator with a side effect of printing stuff is no good in functional programming. 最后,带有打印内容副作用的累加器在函数式编程中并不好。 Ideally you should collect()
a list of duplicates and return it - since your question is so vague, I will trust you can write it yourself with the information above. 理想情况下,您应该collect()
一个重复列表并返回它-由于您的问题如此含糊,我相信您可以使用上面的信息自行编写。
You could so some other interesting things with matrices and streams and duplicates, eg this will take much more coding than two embedded loops: 您还可以使用矩阵,流和重复项进行其他一些有趣的操作,例如,与两个嵌入式循环相比,这将花费更多的代码:
Map<Integer,Long> m = Arrays.stream(matrix)
.parallel()
.flatMapToInt(Arrays::stream)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(m); //prints occurences of each int: {1=3, 3=15, 4=21, 5=1}
In the end, streams are no silver bullet, just a nice help from time to time. 最后,信息流不是灵丹妙药,只是不时的帮助。 Some languages have better syntax and more powerful constructs to adapt functional style, but Java is liked for something else. 某些语言具有更好的语法和更强大的结构来适应功能样式,但是Java在其他方面很受欢迎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.