【发布时间】:2017-09-10 06:17:55
【问题描述】:
我想检查一个数组或列表中所有相似的整数是否被分组。
{2, 2, 5, 5, 5, 1, 7, 7, 5, 7} 应该给 false。而{2, 2, 5, 5, 5, 1, 7, 7, 7} 应该给true。这就是我目前所拥有的,当它应该是false时它会打印true:
public class testing1 {
public static void main(String[] args){
int [] x = {2, 2, 5, 5, 5, 1, 7, 7, 5, 7};
System.out.println(isGrouped(x));
}
public static boolean isGrouped(int[] x){
for(int i = 0; i < x.length; i++){
for(int j = i + 1; j < x.length; j++){
if(x[i] == x[j])
return true;
}
}
return false;
}
}
【问题讨论】:
-
你的代码打印什么
-
你需要检查一组数字
-
@NavneetKrishna 当它应该为假时它打印为真
-
你们有没有读过他的问题?它很清楚,很常见。
-
只要找到每个重复值的出现次数。然后,您必须检查循环内部,以便所有出现的事件都被分组。
标签: java arrays list duplicates grouping