创建 object 类型的二维数组时出错
error creating 2d array of type object
所以,我正在尝试制作一个包含 objects 的板,称为节点,但出于某种原因,当我尝试创建节点数组时,标题中出现错误,但没有明白为什么。从我学到的一切来看,
"public static Node[][] board = new Node[11][11];"
应该是一个有效的声明。我在这里要做的就是创建一个 11x11 的数组。我稍后在循环中填充数组。
我在这里和其他地方寻求帮助,但找不到任何解决问题的方法。有些想法很接近,但问题仍然存在。任何帮助都会很棒。
public class Board {
//creates the board
public static Node[][] board = new Node[11][11];
//create an empty node and place it in every other location
//like a checkered board.
for(int i = 0; i < board.length; i++) {
for (int j = 0; j< board[i].length; j+=2) {
if(i%2 == 0) {//if it is an even row start at 0
board[i][j] = new Node(null);
}else if(j+1 < board[i].length){//if odd row and less than length, start at 1
board[i][j+1] = new Node(false);
}else{
}
}
}
}
你需要一个方法或构造函数,我不相信 Java 让你把语句放在 class 主体中。
public class Board {
// Creates the board
public static Node[][] board = new Node[11][11];
public Board() {
// Create an empty node and place it in every other location
// like a checkered board.
for(int i = 0; i < board.length; i++) {
for (int j = 0; j< board[i].length; j+=2) {
if(i%2 == 0) {
// Even row start at 0
board[i][j] = new Node(null);
}
else if(j+1 < board[i].length) {
// Odd row and less than length, start at 1
board[i][j+1] = new Node(false);
}
}
}
}
}
所以,我正在尝试制作一个包含 objects 的板,称为节点,但出于某种原因,当我尝试创建节点数组时,标题中出现错误,但没有明白为什么。从我学到的一切来看,
"public static Node[][] board = new Node[11][11];"
应该是一个有效的声明。我在这里要做的就是创建一个 11x11 的数组。我稍后在循环中填充数组。
我在这里和其他地方寻求帮助,但找不到任何解决问题的方法。有些想法很接近,但问题仍然存在。任何帮助都会很棒。
public class Board {
//creates the board
public static Node[][] board = new Node[11][11];
//create an empty node and place it in every other location
//like a checkered board.
for(int i = 0; i < board.length; i++) {
for (int j = 0; j< board[i].length; j+=2) {
if(i%2 == 0) {//if it is an even row start at 0
board[i][j] = new Node(null);
}else if(j+1 < board[i].length){//if odd row and less than length, start at 1
board[i][j+1] = new Node(false);
}else{
}
}
}
}
你需要一个方法或构造函数,我不相信 Java 让你把语句放在 class 主体中。
public class Board {
// Creates the board
public static Node[][] board = new Node[11][11];
public Board() {
// Create an empty node and place it in every other location
// like a checkered board.
for(int i = 0; i < board.length; i++) {
for (int j = 0; j< board[i].length; j+=2) {
if(i%2 == 0) {
// Even row start at 0
board[i][j] = new Node(null);
}
else if(j+1 < board[i].length) {
// Odd row and less than length, start at 1
board[i][j+1] = new Node(false);
}
}
}
}
}