一个排序题目 有一数组,如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 次 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.java2000.net/p9635参考这个, 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() + "次"); } }} 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); } } }} 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); } } }} 这种题不需要用到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; } } } 单单从本题来讲用HashMap从哪方面讲都没感觉到好处 这样用log4j对吗?关于继承父类 多大的java类算大 一个痛苦的算法(swing) TTS JSAPI使用 创建Synthesizer时为NULL 请教:Java中如何实现c中sprintf的方便用法 java输入输出的初级问题 多个消费者与一个生产者的问题 blob可以存储声音吗? 学习JAVA时写了个读写Ini文件的类,赏脸的朋友请进来留个话吧…… 有关拓展类的问题 Vector.get(int index)返回值的问题 如何判断OutputStream的write是否成功?
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() + "次");
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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;
}
}
}
用HashMap从哪方面讲都没感觉到好处