一个排序题目

seagullbb 2008-09-22 06:59:01
有一数组,如int[] a={2,3,4,3,5,15,44,33,66,3,7,7,9};将数组排序,并输出如下
2 出现次数为:1 次
3 出现次数为:3 次
4 出现次数为:1 次
5 出现次数为:1 次
7 出现次数为:2 次
...全文
127 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanggc1001 2008-09-22
  • 打赏
  • 举报
回复

import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {
int[] a={2,3,4,3,5,15,44,33,66,3,7,7,9};
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for(int i = 0; i < a.length; i++){
if(!m.containsKey(a[i])){
m.put(a[i], 1);
}else{
m.put(a[i], m.get(a[i])+1);
}
}
}
}

invoke100 2008-09-22
  • 打赏
  • 举报
回复
import java.util.*;

public class Statistics{
public static void main(String args[]) {
int[] a = {2,3,4,3,5,15,44,33,66,3,7,7,9};
Arrays.sort(a);
Map<Integer, Integer> m = new TreeMap<Integer, Integer>();
for(int i = 0; i < a.length; i++) {
int item = a[i];
Integer freq = m.get(item);
m.put(item, freq == null ? 1: freq + 1);
}
Print(m);
}

static void Print(Map m) {
Iterator it = m.entrySet().iterator();
while(it.hasNext()) {
Map.Entry e = (Map.Entry)it.next();
System.out.println(e.getKey() + "出现次数为:::" + e.getValue() + "次");
}
}
}
老紫竹 2008-09-22
  • 打赏
  • 举报
回复
http://www.java2000.net/p9635
参考这个,
良言相告 2008-09-22
  • 打赏
  • 举报
回复
单单从本题来讲
用HashMap从哪方面讲都没感觉到好处
wensheng_zh2007 2008-09-22
  • 打赏
  • 举报
回复
对啊,用hashmap会好一些
良言相告 2008-09-22
  • 打赏
  • 举报
回复
这种题不需要用到Map吧


public static void main(String args[]) {
int[] a = { 2, 3, 4, 3, 5, 15, 44, 33, 66, 3, 7, 7, 9 };
// sort
Arrays.sort(a);
// out
int currentValue = a[0], count = 0;
for (int i : a) {
if (i == currentValue) {
count++;
} else {
System.out.printf("%-3d出现次数为:%-3d\n", currentValue, count);
currentValue = i;
count = 1;
}
}
}

xuhaiyang 2008-09-22
  • 打赏
  • 举报
回复
hashmap
wangydong 2008-09-22
  • 打赏
  • 举报
回复

import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {
int[] a={2,3,4,3,5,15,44,33,66,3,7,7,9};
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for(int i = 0; i < a.length; i++){
if(!m.containsKey(a[i])){
m.put(a[i], 1);
}else{
m.put(a[i], m.get(a[i])+1);
}
}
}
}


youzi530 2008-09-22
  • 打赏
  • 举报
回复
顶一.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧