怎样从一个数组或集合中找到出现次数最多的项 没有这种方法。把数组放进ArrayList,建立一个Literator,然后用嵌套for循环跟踪 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接写,没测过呵呵--------------------public int getMaxItem(int[] setItem){ HashMap hmItem = new HashMap(); int iMaxItem = setItem[0]; int iMaxCount = 1; for(int i=0;i<setItem.length; i++){ String item = setItem[i] + ""; if(hmItem.containsKey(item)){ int count = Integer.parseInt((String)hmItem.get(item)) + 1; hmItem.put(item, count + ""); if(count>iMaxCount){ iMaxItem = item; } } else{ hmItem.put(item, 1 + ""); } } return iMaxItem;} Arrays.binarySearch 比较方便 public static int getMaxItem(int[] array) { HashMap hm = new HashMap(); int maxItem = 0; for(int i =0;i<array.length;i++) { //遍历数组,并用HashMap处理 Integer i325 = new Integer(array[i]); if(hm.containsKey(i325)) { ((Counter)hm.get(i325)).i++; } else { hm.put(i325, new Counter()); } } Iterator it334 = hm.keySet().iterator(); while(it334.hasNext()) { //找出数组中最大值 int i338 = ((Counter)hm.get((Integer)it334.next())).i; if(i338 > maxItem) { maxItem = i338; } } return maxItem; }调用如下:int[] arr157 = {1,1,3,2,3,3,5,5,1,5,5,5};System.out.println(getMaxItem(arr157));结果:5 忘了,还用到这个类,不好意思,呵呵class Counter { //用于Map的value int i=1; public String toString() { return i+""; }} 系统分析与设计,编程我咋整啊! 用jfreechart如何画圆柱状图? java中怎样改变键盘输入? 请问各位大侠,将读取文件时,采用逐个字节读取和逐行读取哪种效率较高? 苦求,一個關於SQL的問題! 请问后缀是.pdg的文件用什么打开! JDBC-ODBC在使用PreparedStatement进行模糊查询时的问题 Hashtable 接下来该看什么书!!! 天生不笨请来取分 急需用JAVA编成的计时器代码!! 高手给一个java写的base64的编解码工具多谢
--------------------public int getMaxItem(int[] setItem){
HashMap hmItem = new HashMap();
int iMaxItem = setItem[0];
int iMaxCount = 1;
for(int i=0;i<setItem.length; i++){
String item = setItem[i] + "";
if(hmItem.containsKey(item)){
int count = Integer.parseInt((String)hmItem.get(item)) + 1;
hmItem.put(item, count + "");
if(count>iMaxCount){
iMaxItem = item;
}
}
else{
hmItem.put(item, 1 + "");
}
} return iMaxItem;
}
比较方便
HashMap hm = new HashMap();
int maxItem = 0;
for(int i =0;i<array.length;i++) { //遍历数组,并用HashMap处理
Integer i325 = new Integer(array[i]);
if(hm.containsKey(i325)) {
((Counter)hm.get(i325)).i++;
}
else {
hm.put(i325, new Counter());
}
}
Iterator it334 = hm.keySet().iterator();
while(it334.hasNext()) { //找出数组中最大值
int i338 = ((Counter)hm.get((Integer)it334.next())).i;
if(i338 > maxItem) {
maxItem = i338;
}
} return maxItem;
}调用如下:
int[] arr157 = {1,1,3,2,3,3,5,5,1,5,5,5};
System.out.println(getMaxItem(arr157));
结果:5
class Counter { //用于Map的value
int i=1;
public String toString() {
return i+"";
}
}