作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不太确定为什么这段涉及字符数组的代码有意义?
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 进行比较?
最佳答案
char
是无符号的 16 位数字类型,当用作数组索引时将扩展为 int
。
charSet[c]
隐式地 charSet[(int) c]
请注意,如果字符串中包含非 ASCII 字符,代码将会失败,因为只有 ASCII字符的 Unicode 代码点范围为 0-127。任何其他 Unicode 字符都会导致 ArrayIndexOutOfBoundsException
。
关于java - 如何将字符数组与 boolean 值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52684537/
我是一名优秀的程序员,十分优秀!