Skip to main content
GameDev.net gamedev.net
🔒 Locked

method undefined by arraylist

Started by ChristianFrantz Aug 25, 2012 at 7:03 PM 5 replies 1.8k views
Original Post
ChristianFrantz
ChristianFrantz
[source lang="java"] for(int i = 0; i < BRICKS.size(); i++)
{
BRICKS.paintBrick(g);
}[/source]

at the BRICKS.paintBrick(g); line i get an error saying that the method paintBrick is undefined for ArrayList

im not exactly sure why, i think i created the arraylist right

public ArrayList BRICKS;

BRICKS = new ArrayList();

and i have the paintBrick method in my class Brick
If you see a post from me, you can safely assume its C# and XNA :)
SiCrane
SiCrane
Did you mean BRICKS.paintBrick(g)?
ChristianFrantz
ChristianFrantz
no i have an arraylist not just an array
If you see a post from me, you can safely assume its C# and XNA :)
Angex
Angex
You can use "List.get(...)", to return the element at a given index in the ArrayList.

[source lang="java"]
for(int i = 0; i < BRICKS.size(); i++)
{
BRICKS.get(i).paintBrick(g);
}
[/source]

Generic collections can also take advantage of the For-Each loop.

E.g.
[source lang="java"]
for (final Brick b : BRICKS)
{
b.paintBrick(g);
}
[/source]
ChristianFrantz
ChristianFrantz
ah your first solution worked.

another problem now.
[source lang="java"] public Rectangle getBrickBounds()
{
for(int i = 0; i < BRICKS.size(); i++)
{
return new Rectangle(Brick.x, Brick.y, Brick.width, Brick.height);
}
}[/source]
im returning a rectangle for each brick in the BRICKS arraylist, but its saying that the variables im returning are not static whatever that means
If you see a post from me, you can safely assume its C# and XNA :)
David.M
David.M
You're trying to reference a static variable which I'm assuming you haven't made in the Brick class. You should be calling for i.x, i.y, i.width, and i.height.
rip-off
rip-off
If you want to return a rectangle for each brick, you need to return an (array)list of rectangles. This could be populated using a foreach loop as Angex demonstrated.

However, it seems you have some fundamental weaknesses in your current understanding of the Java language and in the kind of problem solving you need to be able to do to program effectively. I think you would benefit by putting this graphical application on hold for a while, and write some text mode games to get familiarity with the basic language. You can return to this project when you are comfortable with the core language.

Consider investing in a good book, or borrowing one from your local library, and reading it through and following all the exercises at the end of each chapter.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.