2 回答

TA貢獻1155條經驗 獲得超0個贊
Cell[][]這是。但是請注意,這與二維數組略有不同。它實際上是一個一維數組,其元素都具有類型Cell[]。這意味著您的陣列不必是“矩形”。
Cell[][] cells = new Cell[10][10];做你所期望的,并創建一個 10x10 的矩形陣列。
但是,您可以執行以下操作:
Cell[][] cells = new Cell[10][];
cells[0] = new Cell[1];
cells[1] = new Cell[1000];
...
cells[1][5] = 1; // allowed, since cells[1] is a Cell[] of size 1000
cells[0][5] = 1; // throws ArrayIndexOutOfBoundsException, since cells[0] has size 1
例如,如果您嘗試表示三角形數據結構(例如帕斯卡三角形),這會有所幫助。

TA貢獻1998條經驗 獲得超6個贊
實際上我認為 Array 不會方便,固定大小等。也許在字段中使用帶有 Collections 的額外類會更符合 OOP 風格和方便。
class Board {
List<List<Cell>> hash;
public Cell getCell(int x, int y) {
// might be usefull to copy return value for immutability of hash
return hash.get(x).get(y);
}
public void setCell(Cell cell, int x, int y) {
this.hash.get(x).set(y, cell);
}
}
添加回答
舉報