I would like to count the occurrences of a character (e.g. space:) ' '
in a 2D array using a stream. I tried to find a solution. Here is my code using nested loops:
public int countFreeSpaces() {
int freeSpaces = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (board[j][i] == ' ') freeSpaces++;
}
}
return freeSpaces;
}
source
share