我想知道是否有比我更快的方法来计算二维数组中有多少个零。这就是我现在的做法。
static int zeroInside = 0;
static int[][] board = new int[][]{
{5,0,0,0,1,0,0,7,0},
{1,0,0,0,8,0,3,0,0},
{0,6,0,2,3,0,0,5,0},
{2,5,0,7,0,0,6,3,0},
{9,0,0,3,0,1,0,0,5},
{0,3,1,0,0,8,0,2,7},
{0,2,0,0,9,5,0,1,0},
{0,0,9,0,6,0,0,0,2},
{0,8,0,0,7,0,0,0,6}};
static void initialZeroCounting(){
for (int outer = 0; outer < board.length;outer++){
for(int inner = 0; inner < board[outer].length; inner++){
if (board[outer][inner] ==0){
zeroInside++;
}
}
}
我尝试了这个片段的许多变体,但没有结果。
List<int[]> list = Arrays.asList(board);
int count = Collections.frequency(list, 0);