What exactly does ++array[s.charAt(i) - #39;A#39;] do?(++array[s.charAt(i) - A] 到底是做什么的?)
问题描述
for (int i = 0; i < s.length(); ++i)
{
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
{
++array[s.charAt(i) - 'A'];
}
}
我了解 For 循环.s.length() 是 26,确切地说是 int[26].所以这个循环会发生26次,0-25.如果 i, 0-25 处的字符介于 AZ 之间或为 AZ 它将继续执行 ++array[s.charAt(i) - 'A'];
从我看到的情况来看,它会添加一次数组每个循环,或者每次循环添加一次数组的值,对于 char i 处的字符串,所以第一个是 0 秒是 2,因为数组从 0 开始.所以在 i 的位置添加一个数组 -'A'
是我感到困惑的地方.
I understand the For loop. the s.length() is 26, int[26] to be exact. so this loop will occur 26 times, 0-25. If the Char at i, 0-25 is between or are A-Z it will then proceed to ++array[s.charAt(i) - 'A'];
From what i see it adds array once per loop, or adds the value of array once per loop, for the String at char i so the first one would be 0 second would be 2, because arrays start at 0. so adding an array at location of i -'A'
is where i get confused.
推荐答案
语句 ++array[s.charAt(i) - 'A'];
正在递增索引数组中的值通过 s.charAt(i) - 'A'
.
The statement ++array[s.charAt(i) - 'A'];
is incrementing the value in the array indexed by s.charAt(i) - 'A'
.
这个循环的作用是计算 s
中每个字母出现的次数.
What this loop does is that it counts up the number of occurrences of each letter in s
.
- 'A'
的原因是它移动"了 ascii/unicode 值,因此 A - Z
的值是 0 - 25.因此更适合作为数组索引.
The reason for - 'A'
, is that it "shifts" the ascii/unicode value so that A - Z
have values 0 - 25. And are thus more suitable as an array index.
这篇关于++array[s.charAt(i) - 'A'] 到底是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:++array[s.charAt(i) - 'A'] 到底是做什么的?


基础教程推荐
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01