I am reading from K&B about Strings. For some extra know how, i was reading tutorial from Oracle. I am copying the source code from 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);
}
}
I added point-1 in source code. I was understaning getChars method. When i run it, this program give me ArrayIndexOutOfBoundsException. Here is what i read in String docs.
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Throws: IndexOutOfBoundsException - If any of the following is true: srcBegin is negative. srcBegin is greater than srcEnd srcEnd is greater than the length of this string dstBegin is negative dstBegin+(srcEnd-srcBegin) is larger than dst.length
What is the destBegin? What offset, the documentation is talking about. 1 is a valid offset in destination array. Please help me solve this confusion.
Thanks.