Java 如何检查数组列表中的对象是否具有相同的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16386278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if objects in an array list has the same value?
提问by 3D-kreativ
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
我正在为Android平台做一个简单的游戏。我有 25 个对象由我称为 Circle 的类制成。每个 Circle 对象都有一个字段color
,该字段包含一个 int 数字,表示
- for red
- for blue
- for white
- for yellow and finally
- 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.
每个对象还有一个布尔值,我称之为status
,并且在开始时设置为 false。但是在游戏过程中,一些 Circle 对象status
被设置为 true。
All 25 objects are stored in an ArrayList
that I call listOfCircles
.
所有 25 个对象都存储在ArrayList
我称之为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.
我的问题是,如何检查所有设置为 true 的 Circle 对象是否具有相同类型的颜色代码?假设三个 Circle 对象被设置为 true 并且每个对象color
都是 3,但情况也可能是这三个 Circle 对象中的一个可能具有 1 的值,另外两个 4 ,那么它不是有效匹配。
Some help would be nice!
一些帮助会很好!
采纳答案by flavian
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.
它是这样工作的。它遍历圆圈列表并查找第一个具有status = true
. 当它找到它时,它会将那个圆圈的颜色保存在int color
. 在此后的每一步,如果它找到一个活动圆圈(带有status = true
,它会检查该圆圈的颜色是否与原始圆圈匹配。
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.
如果不是,则意味着并非所有活动圆圈都具有相同的颜色。因为flag
最初设置为true
,所以单个false
就足以确定并非所有活动圆圈的颜色都相同。
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
.
如果没有false
找到,这意味着如果所有活动圆圈的颜色都与第一个活动圆圈相同,那么flag
剩下的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.
回答by M. Haris Azfar
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
// 可以有任何整数,我放了 1 这样你就可以理解了
回答by jma127
A simple for loop suffices, since you're only checking for one color:
一个简单的 for 循环就足够了,因为您只检查一种颜色:
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;
}
回答by William Falcon
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.
将 isValid 标志设置为 true,将颜色标志设置为 -1。然后遍历数组,如果对象设置为 true,则将颜色设置为该数字(确保只设置一次),然后进入另一个 if 语句。在第二个 if 语句中,如果颜色与数字不匹配,则设置为 false 并中断。
Otherwise this loop will finish with true which is a valid set up.
否则这个循环将以 true 结束,这是一个有效的设置。
回答by M. Haris Azfar
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 ");
}
回答by smttsp
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;
}
回答by saurabh mishra
if frequency of first element of arraylist is equal to size of the arraylist,then all items are equal,else not equal
如果 arraylist 的第一个元素的频率等于 arraylist 的大小,则所有项都相等,否则不相等
if ((Collections.frequency(list, list.get(0))) == list.size() ){
System.out.println("string same");
}
else{
System.out.println("string different");
}