0
votes

I am having a little bit of an issue with my brick breaker coursework game. I am trying to make the game produce bricks by using an array however I have ran into a little bit of an error of "array required, but java.util.List found".

Can anyone help?

Class that I am having trouble with:

public void createGameObjects()
{
 synchronized( Model.class )
 {
   ball   = new GameObj(W/2, H/2, BALL_SIZE, BALL_SIZE, Colour.RED );
   bat    = new GameObj(W/2, H - BRICK_HEIGHT*1.5f, BRICK_WIDTH*3, 
                                   BRICK_HEIGHT/4, Colour.GRAY);
   bricks = new ArrayList<>();
   bricks[0] = new GameObj(0,0, BRICK_HEIGHT, BRICK_WIDTH, Colour.BLUE);
 }
}

The error is occurring on the bottom line:

bricks[0] = new GameObj(0,0, BRICK_HEIGHT, BRICK_WIDTH, Colour.BLUE);

Thank you

2
You're using ArrayList object bricks in array[] form which is wrong, for ArrayList you can use bricks.add(new GameObj(--,---,--)); - Omore
An ArrayList is not an array, but you try to use bricks as an array. - Mark Rotteveel
Please don't mutilate posts. If you want to be disassociated from a post, flag for moderator attention. - Mark Rotteveel

2 Answers

1
votes

You are delaring bricks as an ArrayList and trying to access the elements in array way, hence the error. Try changing the below:

bricks = new ArrayList<>();
bricks[0] = new GameObj(0,0, BRICK_HEIGHT, BRICK_WIDTH, Colour.BLUE);

to

bricks = new ArrayList<>();
bricks.add(new GameObj(0,0, BRICK_HEIGHT, BRICK_WIDTH, Colour.BLUE));

This will add the element in the ArrayList.

0
votes

You can't access List elements using [index] (array syantax), rather use add method and assuming that you are loading single Game object into the ArrayList<Game>, you need to add to the bricks List as shown below:

ArrayList<Game> bricks = new ArrayList<>();
bricks.addd(new GameObj(0,0, BRICK_HEIGHT, BRICK_WIDTH, Colour.BLUE));