赞
踩
1.
import java.util.ArrayList;
import java.util.HashSet;
public class NotSame {
public static void statTimes(String param){
if(param==null){
return;
}
//hashset保存不重复的值 因此
HashSet<Character> hSet=new HashSet<Character>();
char[] cs=param.toCharArray();
for(char c:cs){
hSet.add(c);
}
ArrayList<Character> list=new ArrayList<Character>(hSet);
int n=hSet.size();//有多少种字符
int [] times=new int[n];//保存每种字符的出现次数
for(char c:cs){
times[list.indexOf(c)]++;
}
for(int i=0;i<n;i++){
System.out.println("字符"+list.get(i)+"出现了:"+times[i]+"次");
//打印结果
}
}
public static void main(String[] args) {
statTimes("asdasdasdasd!!!@@@###$$$%%^^&&**(())__++||\\");
}
}
2.
import java.util.Arrays;
public class WorkDemo {
public static void main(String[] args) {
String str="abcdzkasdasdfka";//定义字符串
char arr[]=str.toCharArray();//转换成字符数组
Arrays.sort(arr);//排序数组
String temp=new String(arr);//重新产生字符串
//便利统计
for(int startIndex=0;startIndex<str.length();){
char c=temp.charAt(startIndex);//获取第一个相同字符
String t=String.valueOf(c);//把第一个字符转换成字符串
//获取字符最后出现的位置
int lastIndex=temp.lastIndexOf(t);
System.out.println(t+"出现的次数为"+(lastIndex+1-startIndex));
startIndex=lastIndex+1;//下次开始的位置
}
}
}
3.
//实现显示字符串每个字符出现的次数
public class XXX {
public static void main(String[] args) {
String str="asddsaffhgwr";
String zhongjian="";//存储比较完之后的字符
for(int i=0;i<str.length();i++){
int x=0;//记每个不同字符出现的次数
boolean flag=false;//设置布尔值,确定什么时候自增
char c1=str.charAt(i);
for(int b=0;b<=zhongjian.length()-1;b++){
if(c1==zhongjian.charAt(b)){
flag=false;
}
}
for(int m=0;m<=(str.length()-1);m++){
if(c1==str.charAt(m)){
x++;
}
}
if(flag){
System.out.println(c1+"出现的次数:"+x);
}
zhongjian+=c1;
}
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。