答案很簡單:
由於kwatford說你需要做的是這樣的:
board[0][0] = new hexagon(); // or whatever its constructor is
更長說明:
只是爲了進一步擴大。你的二維數組是;指針數組(或Java中的引用)。這是陣列的一行將是什麼樣子馬上此調用board = new hexagon[n][n];
後:
0 1 2 3 4 5 // column index, row index = 0
-------------------------------------------
| | | | | | | | | | // value
--- | ----- | ----- | ---------------------
| | | ...
| | |
| | |
| | |
| | v
| | Null
| v
| Null
v
Null (This means that it points to nothing)
你試過:
board[0][0].value = 'R';
這是與此相同:
null.value = 'R';
您有初始化您使用此線陣:
board = new Hexagon[n][n];
但你仍然需要初始化您的數組中的元素。這將初始化三個第一:
board[0][0] = new hexagon(); // or whatever its constructor is
board[1][0] = new hexagon(); // or whatever its constructor is
board[2][0] = new hexagon(); // or whatever its constructor is
會導致其在一個數組,看起來像這樣:
0 1 2 3 4 5 // column index, row index = 0
-------------------------------------------
| | | | | | | | | | // value
--- | ----- | ----- | ---------------------
| | |
| | |
| | |
| | |
| | v
| | An instance of type Hexigoon (what you get when you type new Hexigon)
| v
| An instance of type Hexigon (what you get when you type new Hexigon)
v
An instance of type Hexigon (what you get when you type new Hexigon)
我記得兩年前敲我的腦袋在桌子上這個確切的問題。我喜歡stackoverflow
發佈實際堆棧跟蹤。 – 2010-07-30 01:31:28
同意。我們對六角形類型一無所知,什麼。值意味着 –
Dan
2010-07-30 01:41:10
Wtf是.value?它不是VBA :) – 2010-07-30 12:37:03