您的位置:首页 > 其它

求字符串中字母出现的次数并排序输出

2011-06-07 22:43 375 查看
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;

import sun.net.www.content.audio.wav;

public class Strplus {
public static void main(String[] args) {
String str = "abdklie;ljkdfksjfdsfjdkslljslfjksjgkjgl";
String str1 = str;
String str2 = "";
char c ;
int a[] = new int[26];
int i =0 ;
int num = 0;
int number;
while (str1.length() > 0) {
c = str1.charAt(0); //返回一个字符
str1 = str1.replaceAll(String.valueOf(str1.charAt(0)), "*"); //把字符转换为字符串?replaceAll(String a,String b)
str2 = str1.replaceAll("//w", ""); // //w表示/w [a~zA~Z0~9]
System.out.println(c + "出现了 " + str2.length() + "次");
if(i<=26){
a[i]= str2.length();
System.out.println(a[i]);
i++;
}

str1 = str1.replaceAll("//*", ""); // //*表示是
}
num = i;
System.out.println("////");
System.out.println("////////");
System.out.println("//*");
for(int j=0;j <num; j++){
for(int k=0;k<num-j-1;k++){
if(a[k]>a[k+1]){
number = a[k];
a[k] = a[k+1];
a[k+1] = number;
//System.out.print('b');
}
}
//System.out.print(a[j]);
//System.out.print('a');
}
for(int j=0; j<num;j++){
System.out.print(a[j]+"");
//System.out.print(j);
}
}
}

另一种方法: (没有很理解,但是可以实现)

import java.util.*;

public class Strmap {

public static void main(String args[]) {

String str = "12345678hfdjkslahfkj932189oiefsjkar94werfdsf";

Map<Character, KeyValue> map = new HashMap<Character, KeyValue>();

char c;

KeyValue kv = null;

for (int i = 0; i < str.length(); i++) {

c = str.charAt(i); //取出字符

kv = map.get(c); //从map里取出字符c

if (kv == null) {

kv = new KeyValue();

kv.ch = c;

kv.count = 1;

map.put(c, kv);

} else {

kv.count++;

}

}

List<KeyValue> list = new ArrayList<KeyValue>(map.values());

Collections.sort(list);

for (KeyValue o : list) {

System.out.println(o.ch + "=" + o.count);

}

}

}

class KeyValue implements Comparable{

char ch;

int count;

public int compareTo(Object obj) {

// TODO Auto-generated method stub

if (obj instanceof KeyValue) {

KeyValue kv = (KeyValue) obj;

return kv.count - this.count;

}

return -1;

}

}

再有一种方法 ,通过ArrayList,HashMap实现

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Test{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String cnt = s.nextLine();
ArrayList<Character> array = new ArrayList<Character>();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
//--------这里是计算字符出现次数并记录↓----------
for (int i = 0; i < cnt.length(); i++) {
char tmp = cnt.charAt(i);
if(map.containsKey(tmp)){
int num = map.get(tmp);
num++;
map.put(tmp, num);
}else{
map.put(tmp, 1);
array.add(tmp);
}
}
//-----下面就是个冒泡--------
for (int i = 0; i < array.size(); i++) {
for (int j = 0; j < array.size()-1-i; j++) {
if(map.get(array.get(j))>map.get(array.get(j+1))){
char tmp = array.get(j);
array.set(j, array.get(j+1));
array.set(j+1, tmp);
}
}
}
System.out.println(map);//这个就是打印字符出现的次数
System.out.println(array);//这个打印排序后的数组,从小到大
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐