Results 1 to 6 of 6
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Post Finding an object of given properties in an ArrayList

    if I have an array list of objects GRects for example. that are filled different colours.
    can i search the ArrayList to discover if there are GRects in the array list that have a getFillColor() == RED

    or better still i'd have a Boolean method that when passed a colour will return true if there is a red GRect in the arraylist

    looking at the documentation i cant see how the contains method will do this.
    perhaps i'm missing something interpreting how the syntax works? or im looking at the wrong method?

    I'm playing with a breakout game
    if all the RED GRects have gone from the arrayList I want to do something.

    my alternative approach would be to have an arraylist for each color of GRects. because it's easy to determine if the Arraylist is empty.
    but that would involve checking if five arraylist instead of one. and i prefer to keep the GRects in one ArrayList if its possible.

    thanks in advance
    Sonny
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You're right that .contains() won't do it. But separate ArrayLists are overkill, I think. Just iterate through your ArrayList and check each GRect in turn, stopping when you get a match. Performance is not likely to be an issue, and wouldn't be significantly mitigated by having multiple ArrayLists.

    Alternatively you could have int instance variables for each color, and decrement them as the GRects are remove()d.

    -Gary-

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Default

    Thanks Gary, i don't know why i didn't think of that. iterating through that is.

    i like the second idea too, will play with both see which works best
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Another way would be to make a class that extends GRect, and override the equals() method to make comparisons based on color. That way, you could use the contains() method to the same effect.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by m00nchile View Post
    Another way would be to make a class that extends GRect, and override the equals() method to make comparisons based on color. That way, you could use the contains() method to the same effect.
    cheers for that, I already have a GRect Class for the bricks
    Breaking out of Breakout! making new classes

    and i'll come back to this idea once i get some time to complete the iteration method
    hopefully tonight
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Glad to see my twisted mind ocasionally produces something usefull :D.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  2. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  3. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  4. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 06:25 PM
  5. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •