详细解读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) { 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,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,‘ ');字符串之间的复制 public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { Arrays.fill(value,' '); } 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,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 + len,count - index); System.arraycopy(str,offset,len); count += len; return this; } 总结 以上就是本文关于源码详细解读AbstractStringBuilder类源码详细解读的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |