详细解读AbstractStringBuilder类源码
因为看StringBuffer和StringBuilder的源码时发现两者都继承了AbstractStringBuilder,并且很多方法都是直接super的父类AbstractStringBuilder的方法,所以还是决定先看AbstractStringBuilder的源码,然后再看StringBuffer和StringBuilder.
位置:java.lang包中
声明:abstractclassAbstractStringBuilderimplementsAppendable,CharSequence
AbstractStringBuilder类有abstract修饰,可知它不能被实例化。
AbstractStringBuilder类有两个子类:StringBuilder和StringBuffer。
字段
/** *Thevalueisusedforcharacterstorage. */ charvalue[]; /** *Thecountisthenumberofcharactersused. */ intcount;
构造器
1、无参构造器
AbstractStringBuilder(){
}
2、创建abstractstringbuilder实现类的对象时指定缓冲区大小为capacity。
AbstractStringBuilder(intcapacity){
value=newchar[capacity];
}
当子类StringBuilder或StringBuffer实例化时,会在构造器中调用此构造器。
扩充容量
voidexpandCapacity(intminimumCapacity)
此方法有包访问权限,类中有多个方法会调用此方法,在容量不足时扩充容量。
源码:
voidexpandCapacity(intminimumCapacity){
intnewCapacity=(value.length+1)*2;
if(newCapacity<0){
newCapacity=Integer.MAX_VALUE;
}elseif(minimumCapacity>newCapacity){
newCapacity=minimumCapacity;
}
value=Arrays.copyOf(value,newCapacity);
}
将缓冲区长度加1乘2的值赋予变量newCapacity,然后将此值与指定的值比较,将较大值确定为缓冲区的新容量;然后调用Arrays类的copyof方法,此方法会创建一个新数组,然后将原数组中的字符全部复制进新数组中。
ensureCapacity(intminimumCapacity)
publicvoidensureCapacity(intminimumCapacity)
确保容量至少等于指定的最小值。如果当前容量小于指定值,则创建新数组,新数组的容量为指定值的两倍加2;如果当前容量不小于指定值,则直接不做处理。
源码:
publicvoidensureCapacity(intminimumCapacity){
if(minimumCapacity>value.length){
expandCapacity(minimumCapacity);
}
}
测试:
StringBuffers=newStringBuffer();
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)来实现的
publicintcodePointAt(intindex){
if((index<0)||(index>=count)){
thrownewStringIndexOutOfBoundsException(index);
}
returnCharacter.codePointAtImpl(value,index,count);
}
getChars方法的实现用的是System.arraycopy()方法
publicvoidgetChars(intsrcBegin,intsrcEnd,char[]dst,intdstBegin)
{
if(srcBegin<0)
thrownewStringIndexOutOfBoundsException(srcBegin);
if((srcEnd<0)||(srcEnd>count))
thrownewStringIndexOutOfBoundsException(srcEnd);
if(srcBegin>srcEnd)
thrownewStringIndexOutOfBoundsException("srcBegin>srcEnd");
System.arraycopy(value,srcBegin,dst,dstBegin,srcEnd-srcBegin);
}
append方法都牵扯到了ensureCapacityInternal()方法和getChars()方法来实现
publicAbstractStringBuilderappend(Stringstr){
if(str==null)
returnappendNull();
intlen=str.length();
ensureCapacityInternal(count+len);
str.getChars(0,len,value,count);
count+=len;
returnthis;
}
使用了Arrays.copyOf()来实现
voidexpandCapacity(intminimumCapacity){
intnewCapacity=value.length*2+2;
if(newCapacity-minimumCapacity<0)
newCapacity=minimumCapacity;
if(newCapacity<0){
if(minimumCapacity<0)//overflow
thrownewOutOfMemoryError();
newCapacity=Integer.MAX_VALUE;
}
value=Arrays.copyOf(value,newCapacity);
}
Arrays.fill(value,count,newLength,‘\0');字符串之间的复制
publicvoidsetLength(intnewLength){
if(newLength<0)
thrownewStringIndexOutOfBoundsException(newLength);
ensureCapacityInternal(newLength);
if(count
delete()仅改变字符串的大小并未真正的删除字符串
publicAbstractStringBuilderdelete(intstart,intend){
if(start<0)
thrownewStringIndexOutOfBoundsException(start);
if(end>count)
end=count;
if(start>end)
thrownewStringIndexOutOfBoundsException();
intlen=end-start;
if(len>0){
System.arraycopy(value,start+len,value,start,count-end);
count-=len;
}
returnthis;
}
学会灵活的运用System.arraycopy()方法
publicAbstractStringBuilderinsert(intindex,char[]str,intoffset,
intlen)
{
if((index<0)||(index>length()))
thrownewStringIndexOutOfBoundsException(index);
if((offset<0)||(len<0)||(offset>str.length-len))
thrownewStringIndexOutOfBoundsException(
"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;
returnthis;
}
总结
以上就是本文关于源码详细解读AbstractStringBuilder类源码详细解读的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!