Jump to content

  • Log In with Google      Sign In   
  • Create Account


method undefined by arraylist


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 423

Like
0Likes
Like

Posted 25 August 2012 - 01:03 PM

[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

If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9492

Like
0Likes
Like

Posted 25 August 2012 - 01:08 PM

Did you mean BRICKS[i].paintBrick(g)?

#3 burnt_casadilla   Members   -  Reputation: 423

Like
0Likes
Like

Posted 25 August 2012 - 01:09 PM

no i have an arraylist not just an array

If you see a post from me, you can safely assume its C# and XNA :)


#4 Angex   Members   -  Reputation: 884

Like
1Likes
Like

Posted 25 August 2012 - 01:50 PM

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, 25 August 2012 - 01:51 PM.


#5 burnt_casadilla   Members   -  Reputation: 423

Like
0Likes
Like

Posted 25 August 2012 - 03:56 PM

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 :)


#6 David.M   Members   -  Reputation: 731

Like
0Likes
Like

Posted 26 August 2012 - 12:04 AM

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.

#7 rip-off   Moderators   -  Reputation: 7977

Like
0Likes
Like

Posted 26 August 2012 - 03:54 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS