1、Java中的append( )方法其實(shí)是創(chuàng)建了一個(gè)新的數(shù)組,擴(kuò)大了長(zhǎng)度,將需要添加的字符串給復(fù)制到這個(gè)新的數(shù)組中。
2、JAVA中Stringbuffer有append( )方法:而Stringbuffer是動(dòng)態(tài)字符串?dāng)?shù)組,append( )是往動(dòng)態(tài)字符串?dāng)?shù)組添加,跟“xxxx”+“yyyy”相當(dāng)‘+’號(hào)。
(資料圖片)
3、跟String不同的是Stringbuffer是放一起的,String1+String2和***.append("yyyy")雖然打印效果一樣,但在內(nèi)存中表示卻不一樣、String1+String2 存在于不同的兩個(gè)地址內(nèi)存,***.append(Stringbuffer2)放再一起。
4、StringBuffer是線(xiàn)程安全的,多用于多線(xiàn)程。
5、擴(kuò)展資料查看StringBuffer的append()方法如圖所示代碼:[emailprotected] synchronized StringBuffer append(String str) {toStringCache = null;***.append(str);return this;}其中toStringCache是Cleared whenever the StringBuffer is modified.2、進(jìn)入AbstractStringBuilder的append()方法public AbstractStringBuilder append(String str) {if (str == null)return appendNull();int len = ***.length();ensureCapacityInternal(count + len);***.getchars(0, len, value, count);count += len;return this;}如果參數(shù)str為空返回appendNull(); 該方法最終返回return this.3、進(jìn)入ensureCapacityInternal()方法private void ensureCapacityInternal(int minimumCapacity) {// overflow-conscious codeif (minimumCapacity - ***.length > 0) {value = ***.copyof(value,newCapacity(minimumCapacity));}}copyOf(char[] original, int newLength)的方法查JDK幫助文檔可知:復(fù)制指定的數(shù)組,復(fù)制具有指定的長(zhǎng)度。
6、4、進(jìn)入String的getChars()方法public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {//0,len=5,value=[hello],count=5if (srcBegin < 0) {throw new StringIndexOutOfBoundsException(srcBegin);}if (srcEnd > ***.length) {throw new StringIndexOutOfBoundsException(srcEnd);}if (srcBegin > srcEnd) {throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);}***.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);}5、最終調(diào)用的是***.arraycopy的方法:public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)/*src - 源數(shù)組。
7、srcPos - 源數(shù)組中的起始位置。
8、dest - 目標(biāo)數(shù)組。
9、destPos - 目的地?cái)?shù)據(jù)中的起始位置。
10、length - 要復(fù)制的數(shù)組元素的數(shù)量。
11、*/***.arraycopy([world], 0, [hello], 5, 5);將指定源數(shù)組中的數(shù)組從指定位置復(fù)制到目標(biāo)數(shù)組的指定位置。
12、參考資料:百度百科-append。
本文分享完畢,希望對(duì)你有所幫助。
關(guān)鍵詞:

營(yíng)業(yè)執(zhí)照公示信息