在网上看到一个Java编程练习的解释时,我看到了以下代码:
int[] count = new int[128];
int length = 0;
for(char c: s.toCharArray()){
if(++count[c] == 2){
length += 2;
count[c] = 0;
}
}
我了解代码的作用,但我不知道它如何使用字符索引(即 count[c],其中 c 是字符)访问数组元素。我以为索引只能是整数?
最佳答案
char 隐式转换为 int。索引仍然是一个 int。
关于java - 在 Java 中使用 char 索引访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57773977/