1
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.

5 Answers 5

6

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 https://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html.

Sign up to request clarification or add additional context in comments.

5 Comments

@Kevin, just curious... why didn't you mark this as the correct answer since it came first and is the one you're thanking?
I slipped @Kevin a twenty. :)
@Mike I didn't want to be petty, but I wondered the same thing.
@Scott -- For what it's worth, I voted you up. Tell you what, I'll split the twenty with you.
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?
3

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.

Comments

1
  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.

Comments

0

Because it is not a RandomOccupant.

6 Comments

but didn't I declare it as an arrayList of type RandomOccupant?
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).
@GregS: What if Treasure derives from RandomOccupant?
RandomOccupant seems like a base class for "stuff you find in a maze"
@Adrian: He's not adding 'this', he's adding new Treasure(this)
|
0

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

randOccupants.add(new Treasure(this));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.