如何检查是否在数组列表对象具有相同的价值呢? [英] How to check if objects in an array list has the same value?

查看:97
本文介绍了如何检查是否在数组列表对象具有相同的价值呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了Android平台的一个简单的游戏。我有,我称之为圈类制作25对象。每个Circle对象具有字段颜色持有一个int数Re presenting

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. 红色

  2. 蓝色

  3. 白色

  4. 黄色,最后

  5. 绿色。

因此​​,有每一种颜色的五个对象

So there a five objects of each color.

每个对象也有,我称之为状态一个布尔值,并将其设置为false开头。但在比赛中,有的圆的物体状态的设置为true。

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.

25的所有对象都存储在一个的ArrayList ,我称之为 listOfCircles

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

我的问题是,我怎么能检查是否被设置为true的所有对象圈具有相同类型的彩色code的?比方说,三圆对象被设置为true,每个对象颜色 3,但情况也有可能是这种三Circle对象的可能值1和另外两个4,那么它是不是一个有效匹配。

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.

一些帮助将是不错!

推荐答案

它的工作原理是这样的。它经过各界的列表,查找具有第一个状态= TRUE 。当它发现它,它节省了那个圈子的 INT颜色的颜色。在其后的每一步,如果发现一个活跃圆(与状态= TRUE ,它会检查,看看是否能圆的颜色匹配的原之一。

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.

这篇关于如何检查是否在数组列表对象具有相同的价值呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆