J.Doe
J.Doe

Reputation: 47

How to correctly use the add method in Java?

I need to add the Rectangle to the ArrayList called bricks as you can see below.

private void drawBrick(int startX, int startY){

    new Rectangle();

    currentColor = 0;

    startX = 54;
    startY = 16;

    bricks = new ArrayList<Rectangle>();
    bricks.add("Rectangle");


}

I keep getting compilation errors after adding that that last line of code and this weird warning pops up saying "Some messages have been simplified; recompile with -Xdiags:verbose to get full output."

Anyone know what I've done wrong?

Upvotes: 0

Views: 1031

Answers (1)

Olathe
Olathe

Reputation: 1895

You can't just refer to the Rectangle you created by using "Rectangle". Instead, you need to name the Rectangle when you create it and use that name later on, like this:

private void drawBrick(int startX, int startY) {
    Rectangle theRectangle = new Rectangle();

    currentColor = 0;

    startX = 54;
    startY = 16;

    bricks = new ArrayList<Rectangle>();
    bricks.add(theRectangle);
}

Notice how that creates a variable called theRectangle which is set equal to the Rectangle you create, then that specific Rectangle (theRectangle) is added to the ArrayList called bricks.

Upvotes: 1

Related Questions