append方法python(append方法)

时间:2022-09-23 09:57:47来源:
导读您好,现在蔡蔡来为大家解答以上的问题。append方法python,append方法相信很多小伙伴还不知道,现在让我们一起来看看吧!1、Java中的append(...

您好,现在蔡蔡来为大家解答以上的问题。append方法python,append方法相信很多小伙伴还不知道,现在让我们一起来看看吧!

1、Java中的append( )方法其实是创建了一个新的数组,扩大了长度,将需要添加的字符串给复制到这个新的数组中。

2、JAVA中Stringbuffer有append( )方法:而Stringbuffer是动态字符串数组,append( )是往动态字符串数组添加,跟“xxxx”+“yyyy”相当‘+’号。

3、跟String不同的是Stringbuffer是放一起的,String1+String2和***.append("yyyy")虽然打印效果一样,但在内存中表示却不一样、String1+String2 存在于不同的两个地址内存,***.append(Stringbuffer2)放再一起。

4、StringBuffer是线程安全的,多用于多线程。

5、扩展资料查看StringBuffer的append()方法如图所示代码:进入append方法@Overridepublic synchronized StringBuffer append(String str) {toStringCache = null;***.append(str);return this;}其中toStringCache是Cleared whenever the StringBuffer is modified.2、进入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;}如果参数str为空返回appendNull(); 该方法最终返回return this.3、进入ensureCapacityInternal()方法private void ensureCapacityInternal(int minimumCapacity) {// overflow-conscious codeif (minimumCapacity - ***.length > 0) {value = ***.copyof(value,newCapacity(minimumCapacity));}}copyOf(char[] original, int newLength)的方法查JDK帮助文档可知:复制指定的数组,复制具有指定的长度。

6、4、进入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、最终调用的是***.arraycopy的方法:public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)***.arraycopy([world], 0, [hello], 5, 5);将指定源数组中的数组从指定位置复制到目标数组的指定位置。

12、参考资料:百度百科-append。

本文就为大家分享到这里,希望小伙伴们会喜欢。

标签:
最新文章