我不太确定为什么这段涉及字符数组的代码有意义?String str1 = "Hello"int[] charSet = new int[128];char[] chars = str1.toCharArray(); for (char c : chars) { // count number of each char in s. if (charSet[c] == 0)++charSet[c]; }我的问题是如何将 char 变量作为 charSet 数组的索引并将其与 0 进行比较?
2 回答
慕容3067478
TA贡献1773条经验 获得超3个赞
带有我的评论的代码。
String str1 = "Hello";
int[] charSet = new int[128];// ascii chars a-z and A-Z go from 65-122 using a 128 array is just being lazy
char[] chars = str1.toCharArray();
for (char c : chars) { //loop though each character in the string
if (charSet[c] == 0)//c is the character converted to int since it's all a-z A-Z it's between 65 and 122
++charSet[c];//if it the character hasn't been seen before set to 1
}
添加回答
举报
0/150
提交
取消