取出一个字符串中各项出现的次数。
*如:字符串:"abcdekka27qoq",
*输出格式为:a(2)b(1)k(2)...
下面是程序
----------------------------------------------------------------------------------
import java.util.ArrayList;
public class TiMu {
public static void main(String[] args) {
String s = "acjxzuhcusaucnxzicsxzcsa4x4dw4d4sad4w4das4dw11sa5dw1ds7ac78xzc7s474xzc51sc1a81c7xz4dqdsssccas";
test(s);
}
public static void test(String s){
//ArrayList<E>
ArrayList ys = new ArrayList();
ArrayList zui = new ArrayList();
//String[] ys = new String[s.length()];;
//int[] zui = new int[s.length()];
for(int i=0;i<s.length();i++){
int number = 0;
String s1 = s.substring(i,i+1);
ys.add(s1);
for(int j=0;j<s.length();j++){
if (s1.equals(s.substring(j,j+1))) {
number ++;
}
}
zui.add(number);
//System.out.println(s1+"("+number+")");
}
printjishu(ys, zui);
}
//输出方法
public static void printjishu(ArrayList s,ArrayList zui){
for(int i=0;i<s.size();i++){
for(int j=0;j<i;j++)
{
if(s.get(i).equals(s.get(j))){
s.remove(i);
zui.remove(i);
i--;
}
}
}
for(int m=0;m<s.size();m++){
System.out.print(s.get(m));
System.out.print("("+zui.get(m)+"),");
}
}
}
|
|