AbstractStringBuilder类源码的示例分析

发布时间:2021-09-10 13:53:13 作者:小新
来源:亿速云 阅读:86

这篇文章给大家分享的是有关AbstractStringBuilder类源码的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

因为看StringBuffer 和 StringBuilder 的源码时发现两者都继承了AbstractStringBuilder,并且很多方法都是直接super的父类AbstractStringBuilder的方法,所以还是决定先看AbstractStringBuilder的源码,然后再看StringBuffer 和 StringBuilder.

位置:java.lang包中

声明: abstract class AbstractStringBuilderimplements Appendable, CharSequence

AbstractStringBuilder 类有abstract 修饰,可知它不能被实例化。

AbstractStringBuilder 类有两个子类:StringBuilder和StringBuffer。

字段

 /**
     * The value is used for character storage.
     */
    char value[];
    /**
     * The count is the number of characters used.
     */
    int count;

构造器

1、无参构造器

AbstractStringBuilder() {
  }

2、创建abstractstringbuilder实现类的对象时指定缓冲区大小为capacity。

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

当子类StringBuilder或StringBuffer实例化时,会在构造器中调用此构造器。

扩充容量

void expandCapacity(int minimumCapacity)

此方法有包访问权限,类中有多个方法会调用此方法,在容量不足时扩充容量。

源码:

 void expandCapacity(int minimumCapacity) {
    int newCapacity = (value.length + 1) * 2;
    if (newCapacity < 0) {
      newCapacity = Integer.MAX_VALUE;
    } else if (minimumCapacity > newCapacity) {
      newCapacity = minimumCapacity;
    }
    value = Arrays.copyOf(value, newCapacity);
  }

将缓冲区长度加1乘2的值赋予变量newCapacity, 然后将此值与指定的值比较,将较大值确定为缓冲区的新容量;然后调用Arrays类的copyof方法,此方法会创建一个新数组,然后将原数组中的字符全部复制进新数组中。

ensureCapacity(int minimumCapacity)

public void ensureCapacity(int minimumCapacity)

确保容量至少等于指定的最小值。如果当前容量小于指定值,则创建新数组,新数组的容量为指定值的两倍加2;如果当前容量不小于指定值,则直接不做处理。

源码:

 public void ensureCapacity(int minimumCapacity) {
    if (minimumCapacity > value.length) {
      expandCapacity(minimumCapacity);
    }
  }

测试:

StringBuffer s = new StringBuffer();
    System.out.println("容量:" + s.capacity());// 容量:16
    s.ensureCapacity(10);
    System.out.println("容量:" + s.capacity());// 容量:16
    s.ensureCapacity(30);
    System.out.println("容量:" + s.capacity());// 容量:34
    s.ensureCapacity(80);
    System.out.println("容量:" + s.capacity());// 容量:80

方法

codePointAt方法中都是用Character.codePointAtImpl(value, index, count)来实现的

public int codePointAt(int index) {
    if ((index < 0) || (index >= count)) {
      throw new StringIndexOutOfBoundsException(index);
    }
    return Character.codePointAtImpl(value, index, count);
  }

getChars方法的实现用的是System.arraycopy()方法

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  {
    if (srcBegin < 0)
      throw new StringIndexOutOfBoundsException(srcBegin);
    if ((srcEnd < 0) || (srcEnd > count))
      throw new StringIndexOutOfBoundsException(srcEnd);
    if (srcBegin > srcEnd)
      throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
    System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  }

append方法都牵扯到了ensureCapacityInternal()方法和getChars()方法来实现

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;
  }

使用了Arrays.copyOf()来实现

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);
  }

Arrays.fill(value, count, newLength, ‘\0');字符串之间的复制

public void setLength(int newLength) {
    if (newLength < 0)
      throw new StringIndexOutOfBoundsException(newLength);
    ensureCapacityInternal(newLength);

    if (count < newLength) {
      Arrays.fill(value, count, newLength, '\0');
    }

    count = newLength;
  }

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;
  }

学会灵活的运用System.arraycopy()方法

 public AbstractStringBuilder insert(int index, char[] str, int offset,
                    int len)
  {
    if ((index < 0) || (index > length()))
      throw new StringIndexOutOfBoundsException(index);
    if ((offset < 0) || (len < 0) || (offset > str.length - len))
      throw new StringIndexOutOfBoundsException(
        "offset " + offset + ", len " + len + ", str.length "
        + str.length);
    ensureCapacityInternal(count + len);
    System.arraycopy(value, index, value, index + len, count - index);
    System.arraycopy(str, offset, value, index, len);
    count += len;
    return this;
  }

感谢各位的阅读!关于“AbstractStringBuilder类源码的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. SQLite源码仓库的示例分析
  2. mysql源码示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:Android工具类ImgUtil选择相机和系统相册的示例分析

下一篇:怎么通过重启路由的方法切换IP地址

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》