當前位置:編程學習大全網 - 源碼下載 - 誰知道java中 toCharArray()方法的原代碼?

誰知道java中 toCharArray()方法的原代碼?

/**

* Converts this string to a new character array.

*

* @return a newly allocated character array whose length is the length

* of this string and whose contents are initialized to contain

* the character sequence represented by this string.

*/

public char[] toCharArray() {

char result[] = new char[count];

getChars(0, count, result, 0);

return result;

}

/**

* Copies characters from this string into the destination character

* array.

* <p>

* The first character to be copied is at index <code>srcBegin</code>;

* the last character to be copied is at index <code>srcEnd-1</code>

* (thus the total number of characters to be copied is

* <code>srcEnd-srcBegin</code>). The characters are copied into the

* subarray of <code>dst</code> starting at index <code>dstBegin</code>

* and ending at index:

* <p><blockquote><pre>

* dstbegin + (srcEnd-srcBegin) - 1

* </pre></blockquote>

*

* @param srcBegin index of the first character in the string

* to copy.

* @param srcEnd index after the last character in the string

* to copy.

* @param dst the destination array.

* @param dstBegin the start offset in the destination array.

* @exception IndexOutOfBoundsException If any of the following

* is true:

* <ul><li><code>srcBegin</code> is negative.

* <li><code>srcBegin</code> is greater than <code>srcEnd</code>

* <li><code>srcEnd</code> is greater than the length of this

* string

* <li><code>dstBegin</code> is negative

* <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than

* <code>dst.length</code></ul>

*/

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {

if (srcBegin < 0) {

throw new StringIndexOutOfBoundsException(srcBegin);

}

if (srcEnd > count) {

throw new StringIndexOutOfBoundsException(srcEnd);

}

if (srcBegin > srcEnd) {

throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);

}

System.arraycopy(value, offset + srcBegin, dst, dstBegin,

srcEnd - srcBegin);

}

  • 上一篇:唐代著名詩人“七絕聖手”王昌齡名人名言
  • 下一篇:關於按鍵精靈自動喊話
  • copyright 2024編程學習大全網