简体   繁体   中英

How can I refer to my two dimensional boolean array as a whole?

The 2d boolean is a static variable if that means anything. I thought that I could refer to just the name of the array, but I get an error for doing that.

public class Game {

static boolean[][] board = new boolean[3][3];
}

public class Computer
{
  if (Game.board = true)
    {
        //code
    }
}

Sure, Game.board is how you refer to the boolean[][] two-dimensional array. But what makes you think you can compare a 3x3 array of boolean s to true directly?

if(Game.board = true) will throw a compiler error because board is a two-dimensional array and thus cannot be assigned a single boolean value.

Also, when comparing make sure you use the comparison operator ==

Perhaps, you want to do something like:

if(Game.board[i][j] == true) where i and j are indexing the array:

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        if(board[i][j] == true)
        {
          // Code
        }
    }
}

You have board declared as static , any particular reason?

Since board[i][j] returns a boolean value, there really isn't a need for the == true :

if(board[i][j])
for (int i = 0; i < 3; i++)
   for (int j = 0; j < 3; j++)
      if(board[i][j] == true){

         //Code

      }

Perhaps you are looking for something like this?

import java.util.Arrays;

class Test
{
    public static void main(String[] args)
    {
        boolean[][] board = {{}, {}, {}};
        // all values in board are set to false by default

        boolean[][] board2 = new boolean[0][3];
        boolean[][] board3 = {{true},{true},{true, false, false}};
        boolean[][] board4 = {{}, {true}, {true, true}, {}};

        printInfo(board, 1);
        printInfo(board2, 2);
        printInfo(board3, 3);
        printInfo(board4, 4);


    }

    public static boolean check(boolean[][] board)
    {
        if(board.length == 0) return false;

        int colLength = 0;
        int elementsInBoard = 0;    

        for (int i = 0; i < board.length; i++)
        {
            colLength = board[i].length;
            elementsInBoard += colLength;
            for(int j = 0; j < colLength; j++)
            {
                if (board[i][j] == false)
                {
                    return false;
                }
            }
        }

        if (elementsInBoard == 0)
        {
            return false;
        }else
        {
            return true;
        }
    }

    public static void printInfo(boolean[][] board, int id)
    {
        System.out.println("Board : " + id);
        System.out.println(Arrays.deepToString(board)); 
        System.out.println(check(board));       
    }
}

Output:

Board : 1
[[], [], []]
false
Board : 2
[]
false
Board : 3
[[true], [true], [true, false, false]]
false
Board : 4
[[], [true], [true, true], []]
true

You declared your variable board as a 2D array of boolean and you also initialize it. static boolean[][] board = new boolean[3][3];

Now here, Game.board = true , you are trying to assign your 2D boolean array variable to a boolean value that is incorrect.

A) Code like this is a bug:

if (someBoolean = true)

You have just silently set the boolean variable instead of testing it. You want:

if (someBoolean == true)

or better yet:

if (someBoolean)


B) A boolean[][] (an array of array of boolean) is not a boolean. Try this:

if (Game.board[x][y])

A couple issues with your code. First, you can't directly compare a boolean[][] with true . You'll need to use a loop. Second, you have to place code statements inside a method. You can't use things like if statements without a method. Here's an example of what you want:

public class Game {
    private boolean[] board = new boolean[3][3];

    public static void main(String[] args) {
        boolean allTrue = true;
        for (int i = 0; i < board.length; i++) {
            boolean[] row = board[i];
            for (int j = 0; j < row.length; j++) {
                allTrue &= row[j];
            }
        }
        System.out.println(allTrue);
    }
}

This ignores some short circuit logic you could do, but hopefully you get the point.

One last thing, a single = is assignment, not comparison. Use == for comparison, such as myInt == 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM