下面是一个使用Java语言的代码示例,用于遍历字符数组以查找字母表中每个字母的出现次数:
public class CountAlphabet {
public static void main(String[] args) {
String str = "Hello World";
int[] count = new int[26]; // 字母表中的字母个数
// 将字符串转换为小写字母,并遍历字符数组
for (char c : str.toLowerCase().toCharArray()) {
if (Character.isLetter(c)) {
count[c - 'a']++; // 根据字母的ASCII码进行计数
}
}
// 打印每个字母的出现次数
for (int i = 0; i < count.length; i++) {
if (count[i] > 0) {
System.out.println((char) ('a' + i) + ": " + count[i]);
}
}
}
}
该代码将字符串转换为小写字母,并使用一个长度为26的整型数组count
来统计每个字母的出现次数。遍历字符数组时,通过判断字符是否为字母,然后根据字母的ASCII码进行计数。最后,打印每个字母的出现次数。