求字符串中字母出现的次数并排序输出
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 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);//这个打印排序后的数组,从小到大 } }
相关文章推荐
- 10、取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- 统计字符串中出现次数最多的字母并输出
- 给定字符串,求出现次数最多的那个字母及次数,如有多个重复则都输出
- java 正则表达式查找某段字符串中所有小写字母开头的单词并统计次数,按出现次数排序
- java面试题:输出字符串每个字母出现的次数
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)..(注意,字母出现的次数,要过滤掉数字)
- Java寻找字符串中出现次数最多的字母和次数、两个字符串中寻找最长子字符串、字符串排序
- 取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- [Java练习]输入一个字符串,输出其中某个小写字母出现的次数
- 给定字符串,求出现次数最多的那个字母及次数,如有多个重复则都输出
- java Map输出字符串中字母出现的次数
- 从文件读取包含数字和字母字符串,统计每个字符出现的次数,将次数输出到另外一个文件
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)..
- 编写一个js函数求一个字符串数组按字符串中各字母出现次数多少的重新排序
- 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为: a(2)b(1)k(2)
- [Java练习]输入一个字符串,输出其中某个小写字母出现的次数
- Java编程机试:获取某一字符串中(只要字母),每一个字母出现的次数输出格式为a(x)b(y)c(z).....m(n)
- 一个字符串中可能包含a~z中的多个字符,如有重复,求出现次数最多的那个字母及次数,如有多个重复最多的则都输出。
- c++输入字符串和子字符串 输出子字符串在字符串中出现的次数 并输出字符串中的字母
- 统计字符串中字母出现的次数,字典形式输出(python)