I want to know if there is a faster way than mine, to count how many zeros are inside a 2d array. Here is how I am doing it now.
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++;
}
}
}
I tried many variation of this snippet but without results.
List<int[]> list = Arrays.asList(board);
int count = Collections.frequency(list, 0);
zeroInsidestatic and your functionvoid, make your function return anint, and use a local variable to do the counting.