2016-02-25 38 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

但是,如此多的问题在这里 –