最近我一直在抨擊的頭這一段代碼:如何停止從overwritng變量循環嵌套的ArrayList
for(int i = 0; i < cols; i++) {
for(int j = 0; j < rows; j++) {
SqrTileNormal temp = new SqrTileNormal(i, j, this, ID.Tile);
setTile(temp);
}
}
//additional info
public void setTile(Tile tile) {
int xPosGrid = tile.getXPosGrid();
int yPosGrid = tile.getYPosGrid();
System.out.println("Coords: (" + xPosGrid + ", " + yPosGrid + ")");
this.tiles.get(xPosGrid).set(yPosGrid, tile);
}
//how the nested array looks like.
protected List<ArrayList<Tile>> tiles;
這是這是爲了填補一個二維數組構造的一部分與SqrTileNormal
。我已經找到了問題所在:for循環的每次迭代不斷改寫先前的重複,所以他們都結束了白衣相同xPosGrid
,你會看到這一點:
我一直在嘗試一些東西,但我通常把覆蓋問題,我不想讓它變得不必要的複雜和漫長。有誰知道這個問題的解決方案?任何幫助,將不勝感激!
編輯:
我有什麼: [NULL,NULL,NULL ...] [NULL,NULL,NULL ...] [(NULL,NULL,NULL ...]
我想要什麼:
我得到: [[(10,0),(10,1),(10,2)...] [(10,0),(10,1), (10,2)...] [(10,0),(10,1),(10,2)] ...]
您可以創建一個小的文本部分輸出您期望的和一個你所得到的小文本部分?這個圖形既小又難以理解。 –
在這裏你走Ben! – Trashtalk
當你希望其他行以'(1,0)','(2,0)'開始時,爲什麼你要第一行從'(0,1)'開始,而不是'(0,0) ,...? – Andreas