我写了一个方法来查找数组的模式编号,数组的长度是10,所以我在数组中有十个数字,所以大小已满。我的问题是,如果模式是多个数字我如何修改我的代码来显示两个数字!查找模式编号的方法
如果我的数组看起来像这样[1,1,1,2,2,2,3,5,6,8]
这种情况下的模式是1和2。在我的代码中,它只打印它获得的第一个模式。 所以它会打印模式是1
public static int arryMode (int [] randomList) {
int maxValue = 0;
int maxCount = 0;
for (int i = 0; i < randomList.length; ++i) {
int count = 0;
for (int j = 0; j < randomList.length; ++j) {
if (randomList[j] == randomList[i]) {
++count;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = randomList[i];
}
}
return maxValue;
}
我试着用此代码工作,但我得到了编译器错误表示,需要的是设置发现INT THX反正 –
对不起它的另一种方式圆 –