zoukankan      html  css  js  c++  java
  • java源码阅读StringBuilder

    1类签名与注释

    public final class StringBuilder
        extends AbstractStringBuilder
        implements java.io.Serializable, CharSequence

    一个可变的字符序列。 此类提供与StringBuffer相同的API,但不保证同步。在单线程中应该优先使用StringBuilder,因为它不需要同步,在大多数实现中将更快。

    StringBuilder的主要实现是append与insert方法,他们是重载的,以便接受任何类型的数据。append方法始终在末尾添加字符,而insert方法在指定位置添加。

    每个StringBuilder都有一个容量,只要StringBuilder中包含的字符序列的长度不超过容量,则不需要分配新的内部缓冲区。 如果内部缓冲区溢出,则会自动变大。

    2基本属性

    StringBuilder是通过char数组实现的,字符数组声明在其父类AbstractStringBuilder中,详情如下

    //存储字符的数组
    char[] value;
    
    //当前数组中已有字符的计数
    int count;

    注意区别count和value.length,前者是已有字符的计数,后者是StringBuilder的容量。

    StringBuilder的默认初始化容量是16。

    3 常用方法

    (1)append方法

    append方法实在StringBuilder末尾添加字符

    StringBuilder的append方法是重载的,以便接受任何类型的数据。下面是append String的方法

    //append String
    public StringBuilder append(String str) {
            super.append(str);
            return this;
        }
    // 父类AbstractStringBuilder中具体实现
    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;
        }

    append String时,首先会检查是否需要扩容(ensureCapacityInternal),然后调用String的getChars方法,将str的字符数组追加到value的后面,最后将当前的字符计数count加上str的长度。ensureCapacityInternal方法如下(AbstractStringBuilder中实现

    private void ensureCapacityInternal(int minimumCapacity) {
            // overflow-conscious code
            if (minimumCapacity - value.length > 0) {
                value = Arrays.copyOf(value,
                        newCapacity(minimumCapacity)); 
            }
        }
    
    private int newCapacity(int minCapacity) {
            // overflow-conscious code
            int newCapacity = (value.length << 1) + 2;
            if (newCapacity - minCapacity < 0) {
                newCapacity = minCapacity;
            }
            return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
                ? hugeCapacity(minCapacity)
                : newCapacity;
        }
    
    private int hugeCapacity(int minCapacity) {
            if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
                throw new OutOfMemoryError();
            }
            return (minCapacity > MAX_ARRAY_SIZE)
                ? minCapacity : MAX_ARRAY_SIZE;
        }

    ensureCapacityInternal首先检查是否需要扩容,只有当count+str.length()的长度大于自身容量的情况下才需要扩容。

    若扩容,则newCapacity方法决定扩容多少,最小扩容的新容量=旧容量*2+2,当这个值还小于count+str.length()时,新容量=count+str.length()

    其他重载的append方法原理差不多,这里就不多说了。

    (2)insert方法

    insert方法可以在StringBuilder指定位置添加字符(串)

    StringBuilder的insert方法是重载的,以便接受任何类型的数据。下面是在指定开始位置insert String的实现

    //指定开始位置insert String
    public StringBuilder insert(int offset, String str) {
            super.insert(offset, str);
            return this;
        }
    
    //父类AbstractStringBuilder中具体实现
    public AbstractStringBuilder insert(int offset, String str) {
            if ((offset < 0) || (offset > length()))
                throw new StringIndexOutOfBoundsException(offset);
            if (str == null)
                str = "null";
            int len = str.length();
            ensureCapacityInternal(count + len);
            System.arraycopy(value, offset, value, offset + len, count - offset);
            str.getChars(value, offset);
            count += len;
            return this;
        }

    底层还是调用System.arraycopy通过数组复制实现的。

    (3)delete

    (4)replace

     

  • 相关阅读:
    streamreader
    python编码问题总结
    linux rz上传文件及出错解决方案
    使用linux远程登录另一台linux
    利用Django实现RESTful API(一)
    windows下搭建vue开发环境
    windows下python虚拟环境virtualenv安装和使用
    SonarQube的安装、配置与使用
    windows下安装Mysql(图文详解)
    静态代码分析工具sonarqube+sonar-runner的安装配置及使用
  • 原文地址:https://www.cnblogs.com/ouym/p/9016945.html
Copyright © 2011-2022 走看看