2016-02-25 33 views
-5
public void generateMap(ArrayList<NonPlayableFighter> weakFoes, ArrayList<NonPlayableFighter> 
strongFoes){ 
    Map[0][0]=new FoeCell(strongFoes[(int)(Math.random()*8)]); 
    for(int i=0;i<15;i++){ 
     Map[(int)(Math.random()*9)][(int)(Math.random()*9)]=new FoeCell(weakFoes[(int)(Math.random()*7)]); 
    } 
    Random rand; 
    int randomNum = rand.nextInt((5 - 3) + 1) + 3; 
    for(int i=0;i<randomNum;i++){ 
     Map[(int)(Math.random()*9)][(int)(Math.random()*9)].equals(Collectible.SENZU_BEAN); 
    } 
    Map[(int)(Math.random()*9)][(int)(Math.random()*9)].equals(Collectible.DRAGON_BALL); 
} 
+2

你收到了什麼錯誤? –

+1

是的。是啊...什麼錯誤? – ifly6

+1

和哪一行? –

回答

1

Map[0][0]=new FoeCell(strongFoes[(int)(Math.random()*8)]); 

不能編譯,因爲你使用數組訪問符號(用方括號:strongFoes[somenumber])上ArrayListArrayList不是一個數組,它是一個List。您不能使用[],您必須調用其上的方法,如get(someNumber)set(someNumber, someObject)

這特定的代碼行的內容應是這樣的:

Map[0][0]=new FoeCell(strongFoes.get((int)(Math.random()*8))); 

你可以得到一個ArrayIndexOutOfBoundsException如果沒有在List足夠的元素。有關如何使用ArrayList的更多信息,請參見http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

+0

我真的很想知道新的FoeCell()可以返回除FoeCell以外的其他任何東西。我不知道'Map'是什麼或者它是什麼,但它不僅僅是ArrayList的問題 –

+0

@ Marc-Andre我猜Map'定義爲'public static FoeCell [] [] Map = ...' –

+0

但是,如此多的問題在這裏 –