本文介绍了++ 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处的Char介于0-25之间或为AZ,则它将继续进行++array[s.charAt(i) - 'A'];从我看到的结果来看,对于char i处的String,每个循环添加一次数组,或每个循环添加一次数组的值.第一个是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']到底是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:10