java.lang.StringBuilder(三)常用方法之append()

    在前面的博客中写了关于append()方法,它是StringBuilder构造过程中常用的方法,在JDK6.0中,append()重载有十几种形式,而其中最常用的是append(String str),其作用是把一个字符串追加到当前的StringBuilder后面,最后返回修改后StringBuilder对象,当str=null,则直接在其后面加上"null"。下面我们分析一下JDK关于它的具体实现的流程。

    1

 public StringBuilder append(String str) {
	super.append(str);//跳到父类的方法中
        return this;//返回修改后的对象
    }

2、 再看看其父类AbstractStringBuilder中append(String str)的实现过程

    /**
     * Appends the specified string to this character sequence.
     * <p>
     * The characters of the <code>String</code> argument are appended, in 
     * order, increasing the length of this sequence by the length of the 
     * argument. If <code>str</code> is <code>null</code>, then the four 
     * characters <code>"null"</code> are appended.
     * <p>
     * Let <i>n</i> be the length of this character sequence just prior to 
     * execution of the <code>append</code> method. Then the character at 
     * index <i>k</i> in the new character sequence is equal to the character 
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less 
     * than <i>n</i>; otherwise, it is equal to the character at index 
     * <i>k-n</i> in the argument <code>str</code>.
     *
     * @param   str   a string.
     * @return  a reference to this object.
     */
    public AbstractStringBuilder append(String str) {
	if (str == null) str = "null";//此时直接把其当成“null”串来处理
        int len = str.length();//取得str的长度
	if (len == 0) return this;//Str=""时,可不再作处理,直接返回原对象
	int newCount = count + len;//当前串的长度(newCount) = 原有串长(count) + str的长度(用于更新StringBuilder的count属性值)
	if (newCount > value.length)//当前对象的长度大于其容量的条件下增加
	    expandCapacity(newCount);//扩展当前的对象的容量,具体见下面
	str.getChars(0, len, value, count);//把串的值依次插入到当前对象有末端(具体如下)
	count = newCount;//更新当前对象的容量属性
	return this;//返回当前对象(对象有内容和属性已修改)
    }

3、  从以上两个方法已经实现了功能,但其中调用的两个重要的方法:expanCapacity(int)和getChars(..),其实现原理相当重要,现源码如下:

   /**
     * This implements the expansion semantics of ensureCapacity with no
     * size check or synchronization.这里没有公步的check,所以用于单线程
     */
    void expandCapacity(int minimumCapacity) {//扩充当前对象的容量
	int newCapacity = (value.length + 1) * 2;//当前对象的内容的长度+1的2部(注意这里它的容量扩充方式)
        if (newCapacity < 0) {//不知为何作负数判断??
            newCapacity = Integer.MAX_VALUE;//返回2^31-1 的常量,表示 int 类型能够表示的最大值。
        } else if (minimumCapacity > newCapacity) {//当“自动扩充”的容量还是小于当前的容量,则把当前容量传给它(再见分配时以当前容量为准)
	    newCapacity = minimumCapacity;//赋值
	}
        value = Arrays.copyOf(value, newCapacity);//把原有char[] value用null补其到newCapacity个数,(见下面例一)
    }

 4、说明:从上面方法可看出,StringBulider在每次调用append()当合并后的长度大于当前容量都会变为value.length(修改前对象的长度)*2,所以这里有性能的注意点,如果明确长度时,尽量在定义StringBuilder时指定其容量值。

5、在看2中方法调用的getChars()方法的,它是String类中的一个处理方法。将字符从此字符串复制到目标字符数组(还是后面再写String时再聊这个)。

 

6、示例一,说明Arrays.copyOf(value, newCapacity)的使用过程,说明:复制指定的数组,截取或用 null 字符填充(如有必要),以使副本具有指定的长度。对于在原数组和副本中都有效的所有索引,这两个数组将包含相同的值。对于在副本中有效而在原数组无效的所有索引,副本将包含 '\\u000'(null)。当且仅当指定长度大于原数组的长度时,这些索引存在。

 

		char[] oldChar ={'a','b'};//定义并初始化一个char数组
		char[] newChar = Arrays.copyOf(oldChar, 3);//取得一个副本newChar,且长度为3,这里就会用null补充
		char[] newCharCut = Arrays.copyOf(oldChar, 1);//取得一个副本newCharCut,长度变小,则直接把后面的部分截取
		System.out.println(newChar[2]);//打印出来为null,
		System.out.println(newChar.length);//打印输出为3,补充一位null
		System.out.println(newCharCut.length);//打印输出为1,截取了最后一们'b'.

 

 

                                                              2011-01-19 卓

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值