3

I'm doing a simple game for the Android platform. I have 25 objects made from the class that I call Circle. Each Circle object has the field color that holds an int number representing

  1. for red
  2. for blue
  3. for white
  4. for yellow and finally
  5. for green.

So there a five objects of each color.

Each object also has a boolean value that I call status, and it's set to false at the beginning. But during the game, some of the Circle objects status are set to true.

All 25 objects are stored in an ArrayList that I call listOfCircles.

My question is, how can I check if all Circle objects that are set to true has the same type of color code? Let's say that three Circle objects are set to true and each objects color are 3, but the case could also be that on of this three Circle object could have the value of 1 and the other two 4, then it's not a valid match.

Some help would be nice!

3
  • You could iterate through the array list and compare them one at a time. It would only take one pass through the array. Or are you looking for a different solution? Commented May 5, 2013 at 15:52
  • No the first option is what I'm looking for. Could you answer with some short code example? Commented May 5, 2013 at 15:53
  • If I'm understanding your question correctly, then my approach would be to use two for loops each comparing the two indexes. Commented May 5, 2013 at 15:54

7 Answers 7

3

And to check if a specific value is there more than once you could use something like this:

if ( (Collections.frequency(portList, x)) > 1 ){
    System.out.println(x + " is in portList more than once ");
} 
Sign up to request clarification or add additional context in comments.

Comments

2

You could use something like this to see how many times a specific value is there:

System.out.println(Collections.frequency(portList, 1));

// there can be whatever Integer, i put 1 so you can understand

Comments

2

if frequency of first element of arraylist is equal to size of the arraylist,then all items are equal,else not equal

    if ((Collections.frequency(list, list.get(0))) == list.size() ){

        System.out.println("string same");
    } 
    else{

        System.out.println("string different");

    }

Comments

1

It works like this. It goes through the list of circles and looks for the first one that has status = true. When it finds it, it saves the color of that circle in int color. At every step thereafter, if it finds an active circle(with status = true, it checks to see if the color of that circle matches the original one.

If it doesn't then it means not all active circles have the same color. Because the flag was originally set to true, a single false is enough to know for sure that not all active circles are of the same color.

If no false is found, which means if all active circles have the same color as the first active circle, then the flag remains true.

List<Circle> listOfCircles = new ArrayList<Circle>();
boolean flag = true;
int color;
for (Circle currentCircle: listOfCircles) {
    if (currentCircle.status == true) {
        if (color == null) {
            color = currentCircle.color;
        } else {
            if (color != currentCircle.color) {
                flag = false;
            };
        };
    };
};
// flag now holds true or false according to your needs.

7 Comments

I don't understand this code! Why Color color!? Then I need a new class?
Better! :) Does this also work if it's only two objects that are set to TRUE or five objects that are set to TRUE?
OK, If flag still is TRUE, then they all have the same color code?
OK! Looks good! Thanks for the help! I will try the code later!
On more thing! Why do you use }; this several times? Isn't it ok with only }
|
0

A simple for loop suffices, since you're only checking for one color:

boolean isValid (ArrayList <Circle> circles) {
    int color = -1;
    for (Circle c : circles) {
        if (c.status) {
            if (color < 0)
                color = c.color;
            if (color != c.color)
                return false;
        }
    }
    return true;
}

Comments

0

Set a isValid flag to true and a color flag to -1. Then iterate through the array and if an object is set to true then set the color to that number (make sure it's only set once) then go into another if statement. In that second if statement if the color doesnt match the number set isvalid to false and break.

Otherwise this loop will finish with true which is a valid set up.

Comments

0
public boolean mymeth(){

    int firstCol = 0; 
    for(int i = 0;listOfObject.length(); i++){
        // determine the color of the first element whose status is set
        if(listOfObject.get(i).status && firstCol == 0){ 
            firstCol = listOfObject.get(i).color; 
       }
       //if that value is not equal to next element who is set then return false; 
        else if(listOfObject.get(i).status && firstCol != 0){
            if(firstCol != listOfObject.get(i).color)
               return false;
        }
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.