JDK 源码:AbstractStringBuilder

JDK 源码:AbstractStringBuilder 类详解

AbstractStringBuilderStringBuilderStringBuffer 的抽象基类,它为可变字符串提供了通用的实现。通过分析 AbstractStringBuilder 的源码,我们可以更好地理解 StringBuilderStringBuffer 的工作原理。

1. 基本属性
abstract class AbstractStringBuilder implements Appendable, CharSequence {
    char[] value;  // 存储字符的数组
    int count;     // 当前字符数
}
  • value:字符数组,用于存储字符串的内容。
  • count:当前字符串的长度。
2. 构造方法

AbstractStringBuilder 提供了几个构造方法来初始化字符数组:

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

AbstractStringBuilder() {
    this(16);
}
  • AbstractStringBuilder(int capacity):指定初始容量的构造方法。
  • AbstractStringBuilder():默认初始容量为 16。
3. 常用方法

AbstractStringBuilder 提供了一些操作字符串的方法,包括 appendinsertdeletereplace 等。

  • append:将指定的数据附加到此字符序列。
public AbstractStringBuilder append(String str) {
    if (str == null) {
        str = "null";
    }
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}
  • insert:将指定的数据插入此字符序列中。
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(0, len, value, offset);
    count += len;
    return this;
}
  • delete:移除此字符序列的子字符串中的字符。
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;
}
  • replace:用指定的 String 中的字符替换此序列的子字符串中的字符。
public AbstractStringBuilder replace(int start, int end, String str) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (start > count)
        throw new StringIndexOutOfBoundsException("start > length()");
    if (start > end)
        throw new StringIndexOutOfBoundsException("start > end");

    if (end > count)
        end = count;
    int len = str.length();
    int newCount = count + len - (end - start);
    ensureCapacityInternal(newCount);

    System.arraycopy(value, end, value, start + len, count - end);
    str.getChars(0, len, value, start);
    count = newCount;
    return this;
}
4. 确保容量

AbstractStringBuilder 通过 ensureCapacityInternal 方法确保容量足够:

private void ensureCapacityInternal(int minimumCapacity) {
    // overflow-conscious code
    if (minimumCapacity - value.length > 0)
        expandCapacity(minimumCapacity);
}

void expandCapacity(int minimumCapacity) {
    int newCapacity = value.length * 2 + 2;
    if (newCapacity - minimumCapacity < 0)
        newCapacity = minimumCapacity;
    if (newCapacity < 0) {
        if (minimumCapacity < 0) // overflow
            throw new OutOfMemoryError();
        newCapacity = Integer.MAX_VALUE;
    }
    value = Arrays.copyOf(value, newCapacity);
}
  • ensureCapacityInternal:检查当前容量是否足够,不足时调用 expandCapacity
  • expandCapacity:扩展字符数组的容量,通常为当前容量的两倍加二。
5. 字符序列

AbstractStringBuilder 实现了 CharSequence 接口,因此提供了 charAtlengthsubSequence 等方法。

  • charAt:返回指定索引处的字符。
public char charAt(int index) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    return value[index];
}
  • length:返回字符序列的长度。
public int length() {
    return count;
}
  • subSequence:返回一个新的字符序列,它是此序列的子序列。
public CharSequence subSequence(int start, int end) {
    return this.substring(start, end);
}
总结

AbstractStringBuilder 为可变字符串提供了基础实现,其子类 StringBuilderStringBuffer 分别提供了非线程安全和线程安全的实现。通过理解 AbstractStringBuilder 的源码,可以更好地使用 StringBuilderStringBuffer 来进行字符串操作,提高代码的性能和可读性。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值