作者zanyking (遥远的旅人)
看板java
标题Re: [问题] 逻辑问题
时间Sat Feb 17 12:20:35 2007
※ 引述《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