1.Javaä¸å符串为ä»ä¹ä¸ä»¥\0ç»å°¾
2.StringBuilder为ä»ä¹çº¿ç¨ä¸å®å
¨ï¼
Javaä¸å符串为ä»ä¹ä¸ä»¥\0ç»å°¾
å ¶å®è¿ä¸ªé®é¢æ²¡æä»ä¹å¥½è¯´çï¼Javaéé¢ä¸åé½æ¯å¯¹è±¡ï¼æ¯å¯¹è±¡çè¯ï¼å符串è¯å®å°±æé¿åº¦ï¼å³ç¶æé¿åº¦ï¼ç¼è¯å¨å°±å¯ä»¥ç¡®å®è¦è¾åºçå符个æ°ï¼å½ç¶ä¹å°±æ²¡æå¿ è¦å»æµªè´¹é£1åèç空é´ç¨ä»¥æ æå符串çç»æäºã
å¦è¿C/C++ç人é½æä¸ç§åºå®çæ维模å¼ï¼å¡æ¯å符串é½æ¯ä»¥\0ç»æï¼å ¶å®C++ä¸æªå¿ ï¼ä¸ä¿¡ä½ æ¥æ¥stringè¿ä¸ªæ³åç±»ï¼ä¸ºäºä¸Cè¯è¨å ¼å®¹æ§ï¼å®éé¢æä¸ä¸ªæ¹æ³c_str()å°±æ¯è¿å以\0ç»æçå符串ã
ä¸é¢æ¯C++/C代ç
char
str[]=âtestâ;//è¿ä¸ªå 5个åè
为ä»ä¹æ¯5个åèï¼å 为Cè¯è¨çæ åè¾åºå½æ°printf对å符串è¿è¡è¾åºæ¶ï¼ä¼å¤æå½åå符æ¯ä¸æ¯\0,源码php相册网站源码å¦ææ¯\0çè¯ï¼å°±åæ¢è¾åºãä¸ä¿¡ï¼ä½ å¯ä»¥ççLinuxä¸çVsprintf.cè¿ä¸ªæ件
Javaä¸ï¼
char
[]str=âtestâ;//ç¼è¯é½éä¸è¿
char
[]str=new
char[];//è¿æ ·æè¡ï¼ä¹å°±æ¯ç´æ¥æå®äºå ¶å¤§å°
å¨javaä¸æ°ç»å ¶å®å°±æ¯ä¸ä¸ªå¯¹è±¡ï¼å¦ä¹ javaæ¶ï¼æ们é½æ¥è§¦çä¸å¥è¯å°±æ¯everythingis
object,å æ¤æ°ç»ä¹ä¸ä¾å¤ï¼æ°ç»å¯¹è±¡éè¿æä¸ä¸ªå±æ§å«ålength,å°±æ¯æ°ç»çé¿åº¦ï¼å æ¤å¯¹äºè¾åºå½æ°æ¥è¯´ï¼æç´æ¥ç大å°å¯ä»¥å¤æå符串çè¾¹çï¼ç¼è¯å¨å°±æ²¡å¿ è¦åå»æµªè´¹ä¸ä¸ªç©ºé´æ è¯å符串çç»æã
Javaä¸çStringç±»
å¦ä¸String
str=âtestâ;为ä»ä¹ä¸è½/0ä½ä¸ºç»æå¢ï¼å ¶å®ä¸é¢ä¹è¯´äºï¼Stringç±»éé¢æ¬èº«å°±ææ¹æ³length()å¯ä»¥ç¡®å®å符串çé¿åº¦ï¼å äºè¾åºæ¶å®å ¨å¯ä»¥æ ¹æ®è¿ä¸ªé¿åº¦æ¥è¾åºï¼å½æ们è°ç¨
System.out.println(str)ï¼çæºç çè¯ï¼çæ£è°ç¨çæ¯å¦ä¸è¿ä¸ªå½æ°ï¼
public
void
write(String
str,
int
off,
int
len)
throws
IOException
{
synchronized
(lock)
{
char
cbuf[]
//å¦æé¿åº¦å°äº1Kçè¯;
if
(len
<=
writeBufferSize)
{
if
(writeBuffer
==
null)
{ //å¦æwriteBufferå±äºç¬¬ä¸æ¬¡ä½¿ç¨ï¼è¿æ²¡ç³è¯·ç¼å空é´æ¶
writeBuffer
=
new
char[writeBufferSize];
}
cbuf
=
writeBuffer;
}//大äº1Kçè¯
else
{
//
Don't
permanently
allocate
very
large
buffers.
cbuf
=
new
char[len];
}//å°stråå ¥ç¼åï¼è¿éçå°äºæ²¡ï¼è¿ä¸ªçlenå°±æ¯å符串çé¿åº¦
str.getChars(off,
(off
+
len),
cbuf,
0);
write(cbuf,
0,
len);//åæ§å¶å°å
}
}
StringBuilder为ä»ä¹çº¿ç¨ä¸å®å ¨ï¼
1ã为ä»ä¹è¾åºå¼è·é¢æå¼ä¸ä¸æ ·æ们å çä¸ä¸StringBuilderç两个æååéï¼è¿ä¸¤ä¸ªæååéå®é ä¸æ¯å®ä¹å¨AbstractStringBuilderéé¢çï¼StringBuilderåStringBufferé½ç»§æ¿äºAbstractStringBuilderï¼
//åå¨å符串çå ·ä½å 容char[] value;//å·²ç»ä½¿ç¨çå符æ°ç»çæ°éint count;
åçStringBuilderçappend()æ¹æ³ï¼
@Overridepublic StringBuilder append(String str) { super.append(str); return this;}
StringBuilderçappend()æ¹æ³è°ç¨çç¶ç±»AbstractStringBuilderçappend()æ¹æ³
public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this;}
æ们å ä¸ç®¡ä»£ç ç第äºè¡å第å è¡å¹²äºä»ä¹ï¼ç´æ¥ç第ä¸è¡ï¼count += lenä¸æ¯ä¸ä¸ªååæä½ãå设è¿ä¸ªæ¶åcountå¼ä¸ºï¼lenå¼ä¸º1ï¼ä¸¤ä¸ªçº¿ç¨åæ¶æ§è¡å°äºç¬¬ä¸è¡ï¼æ¿å°çcountå¼é½æ¯ï¼æ§è¡å®å æ³è¿ç®åå°ç»æèµå¼ç»countï¼æ以两个线ç¨æ§è¡å®åcountå¼ä¸ºï¼èä¸æ¯ãè¿å°±æ¯ä¸ºä»ä¹æµè¯ä»£ç è¾åºçå¼è¦æ¯å°çåå ã
2ã为ä»ä¹ä¼æåºArrayIndexOutOfBoundsExceptionå¼å¸¸ã
æ们çåAbstractStringBuilderçappend()æ¹æ³æºç ç第äºè¡ï¼ensureCapacityInternal()æ¹æ³æ¯æ£æ¥StringBuilder对象çåcharæ°ç»ç容éè½ä¸è½çä¸æ°çå符串ï¼å¦æçä¸ä¸å°±è°ç¨expandCapacity()æ¹æ³å¯¹charæ°ç»è¿è¡æ©å®¹ã
private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) expandCapacity(minimumCapacity);}
æ©å®¹çé»è¾å°±æ¯newä¸ä¸ªæ°çcharæ°ç»ï¼æ°çcharæ°ç»ç容éæ¯åæ¥charæ°ç»ç两ååå 2ï¼åéè¿System.arryCopy()å½æ°å°åæ°ç»çå 容å¤å¶å°æ°æ°ç»ï¼æåå°æéæåæ°çcharæ°ç»ã
void expandCapacity(int minimumCapacity) { //计ç®æ°ç容é int newCapacity = value.length * 2 + 2; //ä¸é´çç¥äºä¸äºæ£æ¥é»è¾ ... value = Arrays.copyOf(value, newCapacity);}
Arrys.copyOf()æ¹æ³
public static char[] copyOf(char[] original, int newLength) { char[] copy = new char[newLength]; //æ·è´æ°ç» System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy;}
AbstractStringBuilderçappend()æ¹æ³æºç ç第å è¡ï¼æ¯å°String对象éé¢charæ°ç»éé¢çå 容æ·è´å°StringBuilder对象çcharæ°ç»éé¢ï¼ä»£ç å¦ä¸ï¼
str.getChars(0, len, value, count);
getChars()æ¹æ³
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { //ä¸é´çç¥äºä¸äºæ£æ¥ ... System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); }
æ·è´æµç¨è§ä¸å¾
å设ç°å¨æ两个线ç¨åæ¶æ§è¡äºStringBuilderçappend()æ¹æ³ï¼ä¸¤ä¸ªçº¿ç¨é½æ§è¡å®äºç¬¬äºè¡çensureCapacityInternal()æ¹æ³ï¼æ¤å»count=5ã
è¿ä¸ªæ¶å线ç¨1çcpuæ¶é´çç¨å®äºï¼çº¿ç¨2继ç»æ§è¡ã线ç¨2æ§è¡å®æ´ä¸ªappend()æ¹æ³åcountåæ6äº
线ç¨1继ç»æ§è¡ç¬¬å è¡çstr.getChars()æ¹æ³çæ¶åæ¿å°çcountå¼å°±æ¯6äºï¼æ§è¡charæ°ç»æ·è´çæ¶åå°±ä¼æåºArrayIndexOutOfBoundsExceptionå¼å¸¸ã
è³æ¤ï¼StringBuilder为ä»ä¹ä¸å®å ¨å·²ç»åæå®äºãå¦ææ们å°æµè¯ä»£ç çStringBuilder对象æ¢æStringBuffer对象ä¼è¾åºä»ä¹å¢ï¼
å½ç¶æ¯è¾åºå¦ï¼
é£ä¹StringBufferç¨ä»ä¹æ段ä¿è¯çº¿ç¨å®å ¨çï¼è¿ä¸ªé®é¢ä½ ç¹è¿StringBufferçappend()æ¹æ³éé¢å°±ç¥éäºã
2024-12-23 06:421486人浏览
2024-12-23 05:462120人浏览
2024-12-23 05:381889人浏览
2024-12-23 05:25213人浏览
2024-12-23 05:14729人浏览
2024-12-23 05:071243人浏览
香港海洋公園的大貓熊盈盈,15號產下一對龍鳳胎,大貓熊的生育年齡以20歲為上限,19歲的盈盈換算成人類的年紀是57歲,創下大貓熊高齡產子的紀錄,由於龍鳳胎體重過輕,生命徵象脆弱,海洋公園和四川貓熊保育
1.Spring Boot中Service用@Transactional 注解在方法上,只在最外层方法起作用吗?2.Spring boot @Service 和@Configuration 的区别?3
1.DEV C++中新建资源文件 源代码2.Dev-C++4编译器中怎么编译并连接源代码啊?(c语言新手)DEV C++中新建资源文件 源代码 有很多方法,这里说一些: 1. 用cmd进入程序