0

我正在尝试使用 Objects 构造一个数组数组。我创建了包含多个变量的对象块。我写:

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[][]

错误信息是:

找不到符号,需要 ']' 并且方法声明无效,需要返回类型

4

1 回答 1

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;
        }
    }
}
于 2012-08-22T07:46:06.953 回答