Re: [問題] 邏輯問題
※ 引述《DavyBlue (失意男)》之銘言:
: : 直接在BBS上寫,沒有想太多,所以可能有錯,就大家找個碴吧。
: http://rafb.net/p/dmswEV84.html
: 剛剛本著好奇心寫寫看
: 本來想用regex 但是這題目好像不太適合
: 用了上面大大的方法寫出來了(不愧是在BBS上打的 問題不少...)
: 順便一提
: http://rafb.net/paste/
: 這裡拿來貼程式碼很好用
: 比直接打在BBS上好得多
DeBUG完的結果也順便PO一下吧,這樣可以讓討論串更加完整。
剛剛重看這個討論串的時候突然想到一個問題。
String to char Array的效率到底如何啊?所以就開了eclipse去看看原始碼。
public char[] toCharArray() {
char result[] = new char[count];
getChars(0, count, result, 0);
return result;
}
所以它new了一個新的char[]來當ans回傳,至於getChars如何運作呢?
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);
}
所以顯然的,他是透過System.arraycopy()來實做的,雖然不知道底層實做
的手段,但至少確定了在JAVA的世界裡,toCharArray()方法效率應該不錯。
--
JAVA 是一個靜態型別reference指定、強物件型別判定的語言。
屬於類C/C++族。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.193.242.79
討論串 (同標題文章)