1

我有以下java位

if(board[i][col].equals(true))
    return false

但是,当我编译它时,我收到以下错误 - “int cannot be dereferenced” - 谁能解释一下这是什么意思?

谢谢!

4

5 回答 5

8

它可能是一组原始类型(int?)。

==,就好了。但如果是int,请确保您没有将其与true:Java 是强类型的。

equals当您想测试两个不同对象的相等性时使用。

于 2013-01-06T17:04:03.533 回答
4
    // Assuming
    int[][] board = new int[ROWS][COLS];

    // In other languages, such as C and C++, an integer != 0 evaluates to true
    // if(board[i][col]) //this wont work, because Java is strongly typed.

    // You'd need to do an explicit comparison, which evaluates to a boolean
    // for the same behavior.
    // Primitives don't have methods and need none for direct comparison:
    if (board[i][col] != 0)
        return false;

    // If you expect the value of true to be 1:
    if (board[i][col] == 1)
        return false;

    // Assuming
    boolean[][] board = new boolean[ROWS][COLS];

    if (board[i][col] == true)
        return false;

    // short:
    if (board[i][col])
        return false;

    // in contrast
    if (board[i][col] == false)
        return false;

    // should be done using the logical complement operator (NOT)
    if (!board[i][col])
        return false;
于 2013-01-06T17:10:14.033 回答
1

使用以下声明:

boolean[][] board = initiate.getChessboard();

您需要使用以下条件:

if(board[i][col] == true)
    return false;

也可以这样写:

if(board[i][col])
    return false;

这是因为equals仅适用于对象,而 boolean 不是对象,它是原始类型。

于 2013-01-06T17:16:59.070 回答
0

如果board是基元数组boolean,请使用

if(board[i][col] == true)
  return false;

或者

if (board[i][col]) return false;

或者

return !board[i][col];
于 2013-01-06T18:15:37.610 回答
0
if(board[i][col])
    return false

如果作为比较的数组boolean[][]是用==. 并且比较== true也可以省略。

于 2013-01-06T17:11:30.773 回答