2016-10-01 43 views
1

在炸彈般的益智遊戲,這些線路都是爲了找出最佳地點投彈(雙板條箱之間)。這是一個13 * 11的網格。座標的ArrayList都應該具有相同的大小

當我檢查2個物體的尺寸,所述一個放養的X是遠遠比另一個大。 他們應該有兩個相同的大小每個座標X來與他的Y.

作爲第一好的動作是在同一個Y軸上,它是ArrayList而不是Set?)。

ArrayList<Integer> bestSpotX=new ArrayList<Integer>();  
ArrayList<Integer> bestSpotY=new ArrayList<Integer>(); 
// ... 
for (int line=0, col=0;line<11 && col<13;line++, col++) {  
    if ((map[line].charAt(col)=='0')&&(map[line+2].charAt(col)=='0')) { 
    bestSpotX.add(col); 
    bestSpotY.add(line+1); 
    }  
} 

我的迭代是錯誤的,還是我添加coord值的方式?

+0

「當我檢查2個物體的尺寸,所述一個放養的X是遠遠比另一個更大的」。你的問題沒有顯示你檢查這個斷言的代碼。從所提供的代碼中可以看出,在循環之前或之後事情可能出錯的機會很多。 –

+0

對不起,我的英文很差,阿德里安。事實上,我確信剩下的代碼,因爲它在迭代在一個簡單的數組上時效果很好,但它要求我對硬件的長度進行「硬編碼」(每個遊戲,包裝箱位置變化以及好的組合) 。這就是我選擇ArrayList的原因。 – GwadaKing

回答

0

爲什麼:

ArrayList<Integer> bestSpotX=new ArrayList<Integer>();  
ArrayList<Integer> bestSpotY=new ArrayList<Integer>(); 

,而不是:

class LineColLocation { 
    protected int line, col; 

// constructors, setters and getters 
} 

ArrayList<LineColLocation> bestSpots=new ArrayList<LineColLocation>();  


int bestSpotLine=...; // ... whatever logic to ... 
int bestSpotCol=...; // determine a *best spot* 

LineColLocation bestSpot=new LineColLocation(bestSpotLine, bestSpotCol); 
bestSpots.add(bestSpot); 

這樣你就不能小心惹的same number of X and Y in two different lists的**隱含這樣的要求* - 您只兌現這個約束(使其明確),不必驗證這個約束,你一定會確信每一次你都不能搞定這些東西。

+0

非常感謝。我是Java的新手,我總是用C語言編寫代碼,並且需要考慮「Object」! – GwadaKing

相關問題