JDK1.8 source code reading AbstractStringBuilder.class
AbstractStringBuilder is an abstract class mainly used for subclass instantiation. Its main inherited classes are StringBuilder, StringBuffer.
Code analysis
Abstract class
abstract class AbstractStringBuilder implements Appendable, CharSequence
There are abstract methods inside, so it must be defined as an abstract class.@Override public abstract String toString();
variable
value stores character strings, and the underlying storage principle of String is the same; count counts the length of character strings for expansion.
char[] value;//存储字符串,String底层也是一样的储存原理
int count;//统计字符串长度
No-argument constructor
No-argument constructor for serialization of subclasses, required
/**
* This no-arg constructor is necessary for serialization of subclasses.
* 无参数构造函数用于子类的序列化,必需的
*/
AbstractStringBuilder() {
}
method
AbstractStringBuilder delete(int start, int end)
Use the shift method to achieve the delete operation, use System.arraycopy to achieve, copy the string after end to the start position, which is equivalent to deleting the middle character.
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)
Move the original character string from offset to len lengths, free the position for the new str, and insert it.
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)
Direct expansion, then insert
@Override
public AbstractStringBuilder append(char c) {
ensureCapacityInternal(count + 1);
value[count++] = c;
return this;
}
AbstractStringBuilder reverse()
Time complexity 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;
}
Test 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