我正在尝试使用Objects构造一个数组数组。我创建了包含多个变量的Object Block。我写道:
Block[][] block = new Block[4][4];
我插入时出现错误:
block[1][1] = new Block(); // As what I've understood, we still need
// to create the object inside this specific block[][]
错误消息是:
无法找到符号,']'预期和无效的方法声明, 需要返回类型
答案 0 :(得分:4)
它对我有用
public class testProgram {
public static void main(String args[]) {
Block[][] block = new Block[4][4];
block[1][1] = new Block();
block[1][1].setName("Block 1 1");
System.out.println(block[1][1].getName());
block[1][2] = new Block();
block[1][2].setName("Block 1 2");
System.out.println(block[1][2].getName());
}
public static class Block {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
答案 1 :(得分:2)