StringBuffer类之详解(一)

StringBuffer类之详解(一)

一、概述

线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。

StringBuffer 上的主要操作是 appendinsert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符添加或插入到字符串缓冲区中。append 方法始终将这些字符添加到缓冲区的末端;而 insert 方法则在指定的点添加字符。

只要发生有关源序列(如在源序列中添加或插入)的操作,该类就只在执行此操作的字符串缓冲区上而不是在源上实现同步

每个字符串缓冲区都有一定的容量。只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无需分配新的内部缓冲区数组。如果内部缓冲区溢出,则此容量自动增大。从 JDK 5 开始,为该类补充了一个单个线程使用的等价类,即 StringBuilder。与该类相比,通常应该优先使用 StringBuilder 类,因为它支持所有相同的操作,但由于它不执行同步(线程不安全),所以速度更快。

二、构造方法

StringBuffer()
          构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
StringBuffer(CharSequence seq)
          public java.lang.StringBuilder(CharSequence seq) 构造一个字符串缓冲区,它包含与指定的 CharSequence 相同的字符。
StringBuffer(int capacity)
          构造一个不带字符,但具有指定初始容量的字符串缓冲区。
StringBuffer(String str)
          构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。

继承于AbstractStringBuilder

构造方法:

AbstractStringBuilder(int capacity) {
    value = new char[capacity];
}

StringBuffer()

public StringBuffer() {
    super(16);
}

StringBuffer(int capacity)

public StringBuffer(int capacity) {
    super(capacity);
}

StringBuffer(String str)

public StringBuffer(String str) {
    super(str.length() + 16);
    append(str);
}

初始容量为指定字符创长度+默认长度16

 StringBuffer(CharSequence seq)

java.lang
接口 CharSequence

所有已知子接口:

Name

所有已知实现类:

CharBuffer, Segment, String, StringBuffer, StringBuilder

CharSequence 是 char 值的一个可读序列。此接口对许多不同种类的 char 序列提供统一的只读访问。

多态的体现

public StringBuffer(CharSequence seq) {
    this(seq.length() + 16);
    append(seq);
}

调用本类中的构造方法

三、序列长度的方法

容器中字符的个数,实际值

int

length()
          Returns the length (character count).

容器的初始容量,理论值

int

capacity()
          Returns the current capacity.

@Override
public synchronized int length() {
    return count;
}

@Override
public synchronized int capacity() {
    return value.length;
}

父类中属性的定义

/**
 * The value is used for character storage.
 */
char[] value;

/**
 * The count is the number of characters used.
 */
int count;

四、添加功能的方法

 StringBuffer append(String str)
          Appends the specified string to this character sequence.
StringBuffer insert(int offset, String str)
          Inserts the string into this character sequence.
append()方法可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
@Override
public synchronized StringBuffer append(String str) {
    toStringCache = null;
    super.append(str);
    return this;
}

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;
}
insert()方法在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
@Override
    public synchronized StringBuffer insert(int offset, String str) {
        toStringCache = null;
        super.insert(offset, str);
        return this;
    }
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;
    }
StringBuffer是字符串缓冲区,当new的时候是在堆内存创建了一个对象,底层是一个长度为16的字符数组
当调用添加的方法时,不会再重新创建对象,在不断向原缓冲区添加字符

五、删除功能的方法

删除指定位置的字符,并返回本身

 StringBuffer deleteCharAt(int index)
          Removes the char at the specified position in this sequence.

删除从指定位置开始指定位置结束的内容,并返回本身

 StringBuffer delete(int start, int end)
          Removes the characters in a substring of this sequence.
@Override
public synchronized StringBuffer delete(int start, int end) {
    toStringCache = null;
    super.delete(start, end);
    return this;
}


@Override
public synchronized StringBuffer deleteCharAt(int index) {
    toStringCache = null;
    super.deleteCharAt(index);
    return this;
}
public AbstractStringBuilder delete(int start, int end) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (end > count)
        end = count;
    if (start > end)
        throw new StringIndexOutOfBoundsException();
    ////删除的时候是包含头,不包含尾
    int len = end - start;
    if (len > 0) {
        System.arraycopy(value, start+len, value, start, count-end);
        count -= len;
    }
    return this;
}

public AbstractStringBuilder deleteCharAt(int index) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    System.arraycopy(value, index+1, value, index, count-index-1);
    count--;
    return this;
}

特殊用法:

sb.delete(0, sb.length());             //清空缓冲区
sb = new StringBuffer();             //不要用这种方式清空缓冲区,原来的会变成垃圾,浪费内存

猜你喜欢

转载自blog.csdn.net/qq_40298054/article/details/84237970
今日推荐