计算字符串中每个字母重复次数,按照次数排序输出。。。运用简单的二维数组冒泡排序...

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]);
}
}

希望大家看一下有什么可以该进的地方??这是一个面试题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值