Strings について K&B から読んでいます。いくつかの追加のノウハウについて、私は Oracle からチュートリアルを読んでいました。Oracle からソース コードをコピーしています。
public class StringDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// put original string in an
// array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] =
palindrome.charAt(i);
}
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] =
tempCharArray[len - 1 - j];
}
String reversePalindrome =
new String(charArray);
System.out.println(reversePalindrome);
//Testing getChars method //1
palindrome.getChars(0, len, tempCharArray, 1);
String tempString = new String(tempCharArray);
System.out.println(tempString);
}
}
ソースコードに point-1 を追加しました。getChars メソッドを理解していました。実行すると、このプログラムから ArrayIndexOutOfBoundsException が返されます。これが私がString docsで読んだものです。
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
例外: IndexOutOfBoundsException - 次のいずれかに該当する場合: srcBegin が負。srcBegin が srcEnd より大きい srcEnd がこの文字列の長さより大きい dstBegin が負である dstBegin+(srcEnd-srcBegin) が dst.length より大きい
destBegin とは何ですか? ドキュメントが話しているオフセットは何ですか。1 は宛先配列の有効なオフセットです。この混乱を解決するのを手伝ってください。
ありがとう。