【问题标题】:Calling a method for a 2d array in java [closed]在java中调用二维数组的方法[关闭]
【发布时间】:2013-02-10 17:54:42
【问题描述】:

我正在为一个涉及骑士之旅的启发式编程课程做作业。目前,我有一种方法,旨在用“棋盘”对象填充 8x8 棋盘数组,这些对象知道棋盘上该空间的“可访问性”,以及该棋盘是否曾经被实际访问过。但是,当我创建 8x8 数组并尝试为此数组调用我的“填充”方法时,我收到一条错误消息,告诉我该数组在我正在使用的包中不存在......我在这里做错了什么?不应该像声明数组和调用方法一样简单

Board [][] chessboard = new Board [8][8];
chessboard.fill();

还是我的语法错误?这里的参考是我的代码,它创建了我的 Board 对象、我的可访问性矩阵,然后将这些可访问性值复制到我的 8x8 数组上的每个 Board 对象。谢谢!

public class Board {

/*
 * Initialize array that emulates chessboard. Will be 8x8, each space will 
 * contain the number of squares from which that space can be reached. The 
 * knight will start at a new space each tour and choose each move based on
 * the "accessability" of each square within its "move pool". The knight 
 * will move to the square with least accesibility each time. When the 
 * Knight is moved to a square, this square will be marked as visited so 
 * that it cannot be visited again. Also, any space that could have been 
 * moved to but was not will have its accesability reduced by 1.     
 */

private boolean visited;
private int accessValue;
private Board [][] chess = new Board[8][8];

public Board(int acessability, boolean beenVisited)
{
    visited = beenVisited;
    accessValue = acessability;  
}

int [][] accessMatrix = {{2,3,4,4,4,4,3,2},
                        { 3,4,6,6,6,6,4,3 },
                        { 4,6,8,8,8,8,6,4 },
                        { 4,6,8,8,8,8,6,4 },
                        { 4,6,8,8,8,8,6,4 },
            { 4,6,8,8,8,8,6,4 },
            { 3,4,6,6,6,6,4,3 },
            { 2,3,4,4,4,4,3,2}};





public void fill()
{

for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[0][i].changeAccess(accessMatrix[0][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[1][i].changeAccess(accessMatrix[1][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[2][i].changeAccess(accessMatrix[2][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[3][i].changeAccess(accessMatrix[3][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[4][i].changeAccess(accessMatrix[4][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[5][i].changeAccess(accessMatrix[5][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[6][i].changeAccess(accessMatrix[6][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[7][i].changeAccess(accessMatrix[7][i]);
}

}   




public int getAccess()
{
return accessValue;
}

public int changeAccess(int newAccess)
{
int accessNew;
accessNew = newAccess;
return accessNew;    
}

【问题讨论】:

  • 棋盘 [] 棋盘 = 新棋盘 [8][8];应该是 Board [][] chessboard = new Board [8][8];
  • chessboard是一个数组,不是Board,所以不能在上面调用fill方法。

标签: java arrays syntax methods 2d


【解决方案1】:

在这段代码中:

Board [] chessboard = new Board [8][8];

Board[8][8] 的类型是Board[][]

所以应该这样写才能编译:

Board [][] chessboard = new Board [8][8];

(那么你必须在你的数组中创建每个 Board() 对象,我把它留给你作为练习)

在这段代码中:

chessboard.fill();

您正在调用 Board 的方法。您只能在 Board 对象上执行此操作,而不能在数组上执行此操作。如果要在数组中的每个板对象上调用此方法,则必须这样做:

for (int i = 0; i <8; i ++) {
    for (int j = 0; j < 8; j ++){
         chessboard[i][j].fill();
    }
}

但我觉得还有更多,因为有些混乱。我认为,棋盘是棋盘,而不是棋盘阵列。您可能想要创建一个棋盘并填充一次。对?然后简单地这样做:

 Board chessboard = new Board();
 chessboard.fill();

【讨论】:

  • 我以为在创建Board类型的数组时,数组的每个元素都填充了一个可以修改的空Board对象?
  • 不行,当你创建一个对象数组(而不是原始类型数组)时,数组的每个元素都是null,所以你必须自己创建。
  • 另外,如果填充方法与称为“chess”的 Board[8][8] 一起使用,我不明白为什么填充方法的类型是 Board 而不是 Board[][]。非常感谢您的帮助!
  • fill 方法在内部与Board[8][8] 或任何类型的任何东西一起工作的事实是无关紧要的。它是Board 类的非静态方法,因此只能在Board 的实例上调用。
  • 我明白了,为了记录,我实际上创建了 64 次棋盘,并且每次在另一个类中重新填充它。有什么方法可以让 fill 方法与 Board[][] 的实例一起工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 2013-11-08
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
相关资源
最近更新 更多