public class Maze
{
public static final int ACTIVE = 0;
public static final int EXPLORER_WIN = 1;
public static final int MONSTER_WIN = 2;
private Square[][] maze;
private ArrayList<RandomOccupant> randOccupants;
private Explorer explorer;
private int rows;
private int cols;
public Maze(Square[][] maze, int rows, int cols, int numTreasures, int numMonsters, String name)
{
int i;
this.maze = maze;
this.cols = cols;
this.rows = rows;
randOccupants = new ArrayList<RandomOccupant>();
for (i = 0; i < numTreasures; i++)
{
randOccupants.add(i) = new Treasure(this); //COMPILE ERROR
}...
爲什麼我不能將其添加到數組列表中?我相信java文檔說,我正確地做到了這一點。Java:添加到數組列表
但我沒有將其聲明爲RandomOccupant類型的arrayList? – dukevin 2010-02-10 00:25:36
更準確地說,你要添加的對象 - 這個 - 是一個迷宮實例,它不是一個RandomOccupant(這是你告訴編譯器randOccupants將包含的內容)。 – 2010-02-10 00:26:09
@GregS:如果Treasure派生自RandomOccupant會怎麼樣? – 2010-02-10 00:26:45