这个抽象类是StringBuilder和StringBuffer的直接父类,而且定义了很多方法,因此在学习这两个类之间建议先学习 AbstractStringBuilder抽象类 该类在源码中注释是以JDK1.5开始作为前两个类的父类存在的,可是直到JDK1.8的API中,关于StringBuilder和StringBuffer的父类还是Object
abstract class AbstractStringBuilder implements Appendable, CharSequence {}
类名用abstract修饰说明是一个抽象类,只能被继承,不能直接创建对象。但是它就一个抽象方法,toString方法。
实现了两个接口:
/**
* The value is used for character storage.
*/
char[] value;
/**
* The count is the number of characters used.
*/
int count;
和String中的变量不同,内部的char[] value,不再是final的了,也就意味着可变
/**
* This no-arg constructor is necessary for serialization of subclasses.
*/
AbstractStringBuilder() {
}
/**
* Creates an AbstractStringBuilder of the specified capacity.
*/
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
AbstractStringBuilder提供两个构造方法,一个是无参构造方法。一个是传一个capacity(代表数组容量)的构造,这个构造方法用于指定类中value数组的初始大小,数组大小后面还可动态改变。
主要是对Value的长度与容量进行的操作
返回已经存储的实际长度(就是count值)
public int length() {
return count;
}
capacity这个单词是’容量’的意思,返回当前value可以存储的字符容量,即在下一次重新申请内存之前能存储字符序列的长度。
public int capacity() {
return value.length;
}
确保value数组的容量是否够用,如果不够用,调用4.expandCapacity(minimumCapacity)方法扩容,参数为需要的容量
public void ensureCapacity(int minimumCapacity) {
if (minimumCapacity > 0)
ensureCapacityInternal(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的二倍(newCapacity)与需要的容量(minimumCapacity)比较。 如果比需要的容量大,那就将容量扩大到容量+1的二倍;如果比需要的容量小,那就直接扩大到需要的容量。 使用Arrays.copyOf()这个非常熟悉的方法来使数组容量动态扩大
如果value数组的容量有多余的,那么就把多余的全部都释放掉
public void trimToSize() {
if (count < value.length) {
value = Arrays.copyOf(value, count);
}
}
强制增大实际长度count的大小,如果 newLength 参数小于当前长度则长度将更改为指定的长度, 截断,数据不变;如果 newLength 参数大于或等于当前长度则将追加有效的 null 字符 (’\u0000’),使长度满足 newLength 参数
public void setLength(int newLength) {
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
if (newLength > value.length)
expandCapacity(newLength);
if (count < newLength) {
for (; count < newLength; count++)
value[count] = '\0';
} else {
count = newLength;
}
}
他们与String中的是一模一样的,代码也是一样的(就有个变量名变动)
复制、将value[]的[srcBegin, srcEnd)拷贝到dst[]数组的desBegin开始处
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);
}
public int indexOf(String str) {return indexOf(str, 0);}
public int indexOf(String str, int fromIndex) {return String.indexOf(value, 0, count, str, fromIndex);}
int indexOf(String str)、int indexOf(String str, int fromIndex)
第一次出现的指定子字符串在该字符串中的索引,可以指定索引
int lastIndexOf(String str)、int lastIndexOf(String str, int fromIndex)
返回最右边出现的指定子字符串在此字符串中的索引 ,也就是最后一个,可以指定索引,指定索引就从索引处 反向匹配
根据索引返回子串
public String substring(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
throw new StringIndexOutOfBoundsException(end);
if (start > end)
throw new StringIndexOutOfBoundsException(end - start);
return new String(value, start, end - start);
}
substring(int start, int end)的简化方法,指定开始位置,默认结束位置为最后
为了实现CharSequence方法,内部调用的substring
@Override
public CharSequence subSequence(int start, int end) {
return substring(start, end);
}
更新方法比较少,因为是数组,数组的访问按照下标进行设置就好了,还提供了替换的功能,也算是更新操作
public void setCharAt(int index, char ch) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
value[index] = ch;
}
使用str替换对象中从start 开始到end结束的这一段
删除指定范围的char
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;
}
删除某个位置的char
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;
}
添加元素,分为尾部追加元素和中间插入元素,由于append与insert都为一系列方法,下列系列中的一部分方法
利用Object(或任何对象)的toString方法转成字符串然后添加到该value[]中
public AbstractStringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
直接修改value[],并且’添加’的意思为链接到原value[]的实际count的后面
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
if (len == 0) return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
str.getChars(0, len, value, count);
count = newCount;
return this;
}
// Documentation in subclasses because of synchro difference
public AbstractStringBuilder append(StringBuffer sb) {
if (sb == null)
return append("null");
int len = sb.length();
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
sb.getChars(0, len, value, count);
count = newCount;
return this;
}
// Documentation in subclasses because of synchro difference
public AbstractStringBuilder append(CharSequence s) {
if (s == null)
s = "null";
if (s instanceof String)
return this.append((String)s);
if (s instanceof StringBuffer)
return this.append((StringBuffer)s);
return this.append(s, 0, s.length());
}
在value[]的下标为index位置插入数组str的一部分,该部分的范围为:[offset,offset+len);
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);
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
System.arraycopy(value, index, value, index + len, count - index);
System.arraycopy(str, offset, value, index, len);
count = newCount;
return this;
}
按照字符进行翻转
public AbstractStringBuilder reverse() {
boolean hasSurrogates = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
char cj = value[j];
char ck = value[k];
value[j] = ck;
value[k] = cj;
if (Character.isSurrogate(cj) ||
Character.isSurrogate(ck)) {
hasSurrogates = true;
}
}
if (hasSurrogates) {
reverseAllValidSurrogatePairs();
}
return this;
}
AbstractStringBuilder就是 可变 字符序列的一个纲领,
它规定了可变字符序列应该有的行为,
比如 添加字符/删除字符/更新字符/获取字符,
因为可变,所以对于可变的支持,自然是必不可少的,
另外,他作为String在很多方面的一个替代,必然也是提供了String的一些功能方法,
否则与String API 变化巨大 也是毫无意义,
因为毕竟本身就是为了描述字符序列。