对字符串中的字符进行相关的统计
package com.xwl;
/**
* 对字符串中的字符进行相关的统计
* @author Administrator
*
*/
public class Algorithm_countString1 {
public static void main(String[] args) {
//字符串
//ctrl+shift+y
String s = "aedsfasdfgAAABBKKK";
/**
* 1.首先创建一个数组26长度的数据
* 2.遍历字符串,根据ASCII A65 a97 进行存入数组中
*/
int[] count = new int[52];
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);//遍历字符串(拿到每个位 上的字母)
char index = (char)(ch - 65); //把对应的字符转换为数组下标A 就是下标为0的位置,值就是出现的次数
count[index]+=1;//把字母转换为对应的下标,对应的数值转换为出现的次数
}
for (int j = 0; j < count.length; j++) {
if(count[j]!=0){
System.out.println("字符:"+ (char)(j+65)+" 出现的次数为:"+count[j]);
}
}
}
}