public void testPX() {
String str = "asd你klfjkl;3你sdjfkl你jakljf你weir3i你2ujzxklnv你";
HashMap map = new HashMap();
while (str.length() != 0) {
String key = String.valueOf(str.charAt(0));
String newstr = str.replaceAll(key, "");
int times = str.length() - newstr.length();
map.put(key, times); // 把字符串--次数放入
str = newstr;
}
Set keys = map.keySet(); // 取出所有字符串
Iterator it = keys.iterator(); // 迭代器
String arr[][] = new String[keys.size()][2];
while (it.hasNext()) { // 遍历
for (int a = 0; a < arr.length; a++) {
String s = (String) it.next();
arr[a][0] = s;
arr[a][1] = "" + map.get(s) + "";
}
}
String temp;
String temp1;
for (int a = 1; a < arr.length; a++) {
for (int b = 0; b < arr.length - a; b++) {
if (Integer.parseInt(arr[b][1]) < Integer
.parseInt(arr[b + 1][1])) {
temp = arr[b][0];
temp1 = arr[b][1];
arr[b][0] = arr[b + 1][0];
arr[b][1] = arr[b + 1][1];
arr[b + 1][0] = temp;
arr[b + 1][1] = temp1;
}
}
}
for (int i = 0, len = arr.length; i < len; i++) {
System.out.println(arr[i][0]+"\t"+arr[i][1]);
}
}
希望大家看一下有什么可以该进的地方??这是一个面试题。
String str = "asd你klfjkl;3你sdjfkl你jakljf你weir3i你2ujzxklnv你";
HashMap map = new HashMap();
while (str.length() != 0) {
String key = String.valueOf(str.charAt(0));
String newstr = str.replaceAll(key, "");
int times = str.length() - newstr.length();
map.put(key, times); // 把字符串--次数放入
str = newstr;
}
Set keys = map.keySet(); // 取出所有字符串
Iterator it = keys.iterator(); // 迭代器
String arr[][] = new String[keys.size()][2];
while (it.hasNext()) { // 遍历
for (int a = 0; a < arr.length; a++) {
String s = (String) it.next();
arr[a][0] = s;
arr[a][1] = "" + map.get(s) + "";
}
}
String temp;
String temp1;
for (int a = 1; a < arr.length; a++) {
for (int b = 0; b < arr.length - a; b++) {
if (Integer.parseInt(arr[b][1]) < Integer
.parseInt(arr[b + 1][1])) {
temp = arr[b][0];
temp1 = arr[b][1];
arr[b][0] = arr[b + 1][0];
arr[b][1] = arr[b + 1][1];
arr[b + 1][0] = temp;
arr[b + 1][1] = temp1;
}
}
}
for (int i = 0, len = arr.length; i < len; i++) {
System.out.println(arr[i][0]+"\t"+arr[i][1]);
}
}
希望大家看一下有什么可以该进的地方??这是一个面试题。