ニコ:
私はいくつかの部分は、私は理解していないコードを検出しました。これは、文字列内の文字の数を維持するとは何かを持っています。私は取得しないという部分をコメントしています。私は任意の助けをいただければ幸いです。ありがとうございました!
私はオンラインそれを見てみましたが、どれも私の質問に答えるように見えるん。
public class test2 {
static int[] inventory;
public static final int ALPHABET = 26;
public static void main(String[] args) {
inventory = new int [ALPHABET];
String dog = "There goes the dog!";
int size = count(dog);
System.out.println(size);
}
private static int count(String data) {
data = data.toLowerCase();
int size = 0;
for (int i = 0; i < data.length(); i++) {
char ch = data.charAt(i);
if (Character.isLetter(ch)) {
size++;
inventory[ch - 'a']++; // this I don't get
}
}
return size;
}
}
Kartik:
System.out.println(0 + 'c'); //ASCII value of 'c'; will print 99
System.out.println(0 + 'a'); //ASCII value of 'a'; will print 97
System.out.println('c' - 'a'); //Difference of ASCII values of characters; will print 99-97=2
あなたのケースでは
inventory[ch - 'a']++;
ch
いくつかの文字になります。
ch - 'a'
「A」からその文字の距離になります。例えば、上記のように'c' - 'a' = 2
。
inventory[ch - 'a']
インデックスの数を指しますch - 'a'
配列に。
inventory[ch - 'a']++
1によってその値をインクリメントします。