0

This is my java code:

class soduku
{
    int[][] board ={
         {8, 0, 0, 0, 0, 0, 0, 0, 0},
         {0, 0, 3, 6, 0, 0, 0, 0, 0},
         {0, 7, 0, 0, 9, 0, 2, 0, 0},
         {0, 5, 0, 0, 0, 7, 0, 0, 0},
         {0, 0, 0, 0, 4, 5, 7, 0, 0},
         {0, 0, 0, 1, 0, 0, 0, 3, 0},
         {0, 0, 1, 0, 0, 0, 0, 6, 8},
         {0, 0, 8, 5, 0, 0, 0, 1, 0},
         {0, 9, 0, 0, 0, 0, 4, 0, 0}};
    int[][] mutable = mutable();
    int[][] mutable()
    {
        int i,j;
        int count = 0;
        for(i=0;i<board.length;i++)
        {
            for(j=0; j<board[i].length;j++)
            if (board[i][j] == 0)
            count++;
        }
        int [][] mutable = new int[count][2];
        int k = 0;
        for(i=0;i<board.length;i++)
        {
            for(j=0; j<board[i].length; j++)
            {
                if(board[i][j]==0)
                {
                    mutable[k][0]=i;
                    mutable[k][1]=j;
                    k++;
                }
            }
        }
        return mutable;
    }
}

As you can see I have to count the number of zeroes so that I can declare the array mutable. I want to know if there's a better way so that could do the same thing. I am new to Java and only know about arrays and some basic data type.

Nothing special
  • 415
  • 3
  • 8

0 Answers0