views:

98

answers:

5
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
  }...

Why can't I add this to the arraylist? I believe the java docs says that I'm doing this correctly.

A: 

Because it is not a RandomOccupant.

GregS
but didn't I declare it as an arrayList of type RandomOccupant?
Kevin Duke
To be more precise, the object you're adding -- this -- is an instance of Maze, which is not a RandomOccupant (which is what you told the compiler randOccupants would contain).
Adrian Petrescu
@GregS: What if Treasure derives from RandomOccupant?
Scott Smith
RandomOccupant seems like a base class for "stuff you find in a maze"
Jimmy
@Adrian: He's not adding 'this', he's adding **new Treasure(this)**
Scott Smith
In the code as posted, he's adding `i` and then trying to assign something to the return value.
Anon.
+6  A: 

You can do either:

randOccupants.add( i, new Treasure(this) );

...or...

randOccupants.add( new Treasure(this) );

Both are equivalent, since you're always appending the new element to the end of the array.

see http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

Scott Smith
thank you, that worked!
Kevin Duke
@Kevin, just curious... why didn't you mark this as the correct answer since it came first and is the one you're thanking?
Mike Sherov
I slipped @Kevin a twenty. :)
JacobM
@Mike I didn't want to be petty, but I wondered the same thing.
Scott Smith
@JacobM - LOL!!
Scott Smith
@Scott -- For what it's worth, I voted you up. Tell you what, I'll split the twenty with you.
JacobM
you don't have a twenty to split because you gave it to me remember? Scott's answer was the one that ultimately helped me since I read it first, but Jacob's had a teeny bit of insight that made it slightly "better". I thought common practice is to select the "best answer" but should I select the quickest helpful reply instead?
Kevin Duke
A: 

Because you're using the add method wrong. You would call:

randOccupants.add(new Treasure(this));
Kaleb Brasee
+3  A: 

First, Treasure would need to either inherit from RandomOccupant or implement it (if it is an interface).

Second, if you want to add it at a particular point in the list, the syntax is

randOccupants.add(i,new Treasure(this));

Although it's hard to see why you don't just do

randOccupants.add(new Treasure(this));

since the items will be added in order even if you don't specify a location.

JacobM
+1  A: 
  1. You're trying to add a Treasure to an ArrayList of RandomOccupants, so unless Treasure is a RandomOccupant, that's not going to work. The given code does not make it clear whether Treasure is a subclass of RandomOccupant, so it's not possible from the code here to say whether that's part of the problem.

  2. What you're actually doing is adding an int to the list here, which is definitely not a RandomOccupant.

  3. ArrayList's add() method returns either boolean or void depending on the version that you're using, so you can't assign to it. You're using the method incorrectly.

The two versions of add() are:

boolean  add(E e)
void     add(int index, E element)

The second version inserts the element at the specified position, and the first one inserts it at the end. Presumably, what you're intending to do is this:

for(i = 0; i < numTreasures; ++i)
    randOccupants.add(new Treasure(this));

But of course, that assumes that Treasure is a subclass of RandomOccupant. If it isn't, then you'll need to change the type of the ArrayList for it to hold Treasures.

Jonathan M Davis