JDK1.8源码阅读AbstractStringBuilder.class
AbstractStringBuilder是一个抽象类主要给子类实例化用的。它的主要继承类有StringBuilder、StringBuffer。
代码分析
抽象类
abstract class AbstractStringBuilder implements Appendable, CharSequence
内部有抽象方法,所以必须定义为抽象类。@Override public abstract String toString();
变量
value存储字符串,String底层也是一样的储存原理;count统计字符串长度,用于扩容。
char[] value;//存储字符串,String底层也是一样的储存原理
int count;//统计字符串长度
无参构造函数
无参数构造函数用于子类的序列化,必需的
/**
* This no-arg constructor is necessary for serialization of subclasses.
* 无参数构造函数用于子类的序列化,必需的
*/
AbstractStringBuilder() {
}
方法
AbstractStringBuilder delete(int start, int end)
通过shift方法实现删除操作,利用System.arraycopy来实现,把 end 后面的字符串复制到 start 位置,即相当于将中间的字符删掉。
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;
}
AbstractStringBuilder insert(int offset, String str)
将原字符串从offset开始向移动len个长度,空出位置给新的str,再插入。
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);//把 offset 后面的字符串复制到 offset+len 位置
str.getChars(value, offset);//将 str 放到 offset 位置,完成插入操作
count += len;//更新 count
return this;
}
String.class参考:
void getChars(char dst[], int dstBegin) {
System.arraycopy(value, 0, dst, dstBegin, value.length);
}
AbstractStringBuilder append(char c)
直接扩容,然后插入
@Override
public AbstractStringBuilder append(char c) {
ensureCapacityInternal(count + 1);
value[count++] = c;
return this;
}
AbstractStringBuilder reverse()
时间复杂度O(n/2)
public AbstractStringBuilder reverse() {
boolean hasSurrogates = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; j--) {//num >> 1,相当于num除以2
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;
}
测试reverse
package test;
public class reverse {
public static void main(String[] args) {
String a="dahouzi";
char[] value = a.toCharArray();
int count = a.length();
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;
System.err.println(j+"---"+value[j]);
System.err.println(k+"---"+value[k]);
}
}
}
输出:
2---u
4---h
1---z
5---a
0---i
6---d