Jump to content
  • Advertisement
Sign in to follow this  
ChristianFrantz

method undefined by arraylist

This topic is 2217 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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<Brick>

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

public ArrayList<Brick> BRICKS;

BRICKS = new ArrayList<Brick>();

and i have the paintBrick method in my class Brick

Share this post


Link to post
Share on other sites
Advertisement
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] Edited by Angex

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!