What exactly does ++array[s.charAt(i) - 'A'] do?

前端 未结 5 974
遥遥无期
遥遥无期 2020-12-30 16:26
for (int i = 0; i < s.length(); ++i) 
    {
        if (s.charAt(i) >= \'A\' && s.charAt(i) <= \'Z\') 
        {
            ++array[s.charAt(i) - \         


        
5条回答
  •  轮回少年
    2020-12-30 16:46

    array seems to be a "counter per capital letter". By subtracting character 'A' from an arbitrary character in a string, you get the letter's index in the array:

    'A' - 'A' == 0
    'B' - 'A' == 1
    'C' - 'A' == 2
    

    To understand this, you should understand, that Java treats char the same as (unsigned) short. Hence, you can make calculations with char

提交回复
热议问题