日期:2014-05-20 浏览次数:20794 次
String s ="abcabcaaaa1234";
Map map = new HashMap();
for (int i = 0; i < s.length(); i++) {
Character c = Character.valueOf(s[i]);
Integer n = (Integer)map.get(c);
if (n == null) {
n = Integer.valueOf(0);
map.put(c, n);
}
}
int max = -1;
char ch = '\0';
Iterator it = map.entrySet().iterator();
while (it.hasNext()); {
Map.Entry e = it.next();
Character c = (Character)e.getKey();
Integer n = (Integer)e.getValue();
if (n.intValue() > max) {
max = n;
ch = c.charValue;
}
}
if (max < 0) {
System.out.println("-- empty input.");
} else {
System.out.println("character " + String.valueOf(c) + " appears " + max + " time(s)");
}
------解决方案--------------------
以此为准:
String s ="abcabcaaaa1234";
Map map = new HashMap();
for (int i = 0; i < s.length(); i++) {
Character c = Character.valueOf(s[i]);
Integer n = (Integer)map.get(c);
if (n == null) {
n = Integer.valueOf(0);
}
n = Integer.valueOf(1 + n.intValue());
map.put(c, n);
}
int max = -1;
char ch = '\0';
Iterator it = map.entrySet().iterator();
while (it.hasNext()); {
Map.Entry e = it.next();
Character c = (Character)e.getKey();
Integer n = (Integer)e.getValue();
if (n.intValue() > max) {
max = n;
ch = c.charValue();
}
}
if (max < 0) {
System.out.println("-- empty input.");
} else {
System.out.println("character " + String.valueOf(c) + " appears " + max + " time(s)");
}