美文网首页
StringBuilder

StringBuilder

作者: 茶瓯 | 来源:发表于2018-06-28 14:35 被阅读0次

    String里面封装了char

    public final class String{
         /** The value is used for character storage. */
          private final char value[];
    
       public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
        if (srcBegin < 0) {
            throw new StringIndexOutOfBoundsException(srcBegin);
        }
        if (srcEnd > value.length) {
            throw new StringIndexOutOfBoundsException(srcEnd);
        }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
        }
        //本地方法
        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
      }
    }
    
    public  class StringBuilder{
       public AbstractStringBuilder append(String str) {
        if (str == null)
            return appendNull();
        int len = str.length();
        //保证char的数组能满足需求
        ensureCapacityInternal(count + len);
        //调用String.getChars()方法
        str.getChars(0, len, value, count);
        count += len;
        return this;
        }
    }

    相关文章

      网友评论

          本文标题:StringBuilder

          本文链接:https://www.haomeiwen.com/subject/tgpgyftx.html