Builder类源码阅读
AbstractStringBuilder类的作用
AbstractStringBuilder是一个抽象类,也是StringBuilder和StringBuffer类的父类,这个类是这两个类的共同点的体现。
AbstractStringBuilder类的类图
从类图中可以看出,
该类实现了Appendable接口,它的实现类的对象能够被添加 char 序列和值。如果某个类的实例打算接收取自 java.util.Formatter 的格式化输出,那么该类必须实现 Appendable 接口。
该类实现了CharSequence接口,CharSequence 是 char 值的一个可读序列。此接口对许多不同种类的 char 序列提供统一的只读访问
AbstractStringBuilder类的重点方法
属性变量
/**
* The value is used for character storage.
*/
char[] value;
/**
* The count is the number of characters used.
*/
int count;
1
2
3
4
5
6
7
8
9
构造方法
/**
* 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];
}
1
2
3
4
5
6
7
8
9
10
11
12
构造方法也是非常简单,不过这里有个容易混淆的变量,capacity代表的容量,length代表的是长度,这两个变量是不同的。
精华方法
public void ensureCapacity(int minimumCapacity)
// 确保容量大小
public void ensureCapacity(int minimumCapacity) {
// 如果minimumCapacity 大于0,就开始内部确认容量
if (minimumCapacity > 0)
ensureCapacityInternal(minimumCapacity);
}
// 内部确认容量
private void ensureCapacityInternal(int minimumCapacity) {
// 如果minimumCapacity > value.length ,那么确定扩容
if (minimumCapacity - value.length > 0)
expandCapacity(minimumCapacity);
}
// 扩容方法
void expandCapacity(int minimumCapacity) {
// 将容量扩大到原来的2倍+2
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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
从源码中分析可以得知:他采取的扩容策略是:length*2+2,如果没有达到要求,会将需要的容量设置为容量值。
个人认为:这个方法是这个类中的精华,也是比较多人会提起的。
常用方法
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此序列复制到目标字符数组 dst。要复制的第一个字符在索引 srcBegin 处;要复制的最后一个字符在索引 srcEnd-1 处。要复制的字符总数为 srcEnd-srcBegin。要复制到 dst 子数组的字符从索引 dstBegin 处开始,
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);
}
1
2
3
4
5
6
7
8
9
10
public AbstractStringBuilder append(String str)
按顺序追加 String 变量中的字符,使此序列增加该变量的长度。如果 str 为 null,则追加 4 个字符 “null”。
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
//如果需要扩容,会在这个方法中进行扩容
ensureCapacityInternal(count + len);
// 将str中的值在value后添加
str.getChars(0, len, value, count);
// 改变长度
count += len;
return this;
}
1
2
3
4
5
6
7
8
9
10
11
这个方法中需要注意的是扩容机制,因为如果不需要扩容的话,这个方法是很简单且高效的。
public AbstractStringBuilder delete(int start, int end)
移除此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。如果 start 等于 end,则不发生任何更改。
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;
}
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
对于需要移除的序列,并没有做一个清空的操作,只是将后面的值覆盖前面的值,并将长度缩减而已。
public AbstractStringBuilder replace(int start, int end, String str)
使用给定 String 中的字符替换此序列的子字符串中的字符。该子字符串从指定的 start 处开始,一直到索引 end - 1 处的字符,如果不存在这种字符,则一直到序列尾部。先将子字符串中的字符移除,然后将指定的 String 插入 start。(如果需要,序列将延长以适应指定的字符串。)
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
//如果有需要,需要扩容
ensureCapacityInternal(newCount);
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(value, start);
count = newCount;
return this;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
这个方法的大概操作是这样的:先将value的值分为三段,第一段是删除区前面的称为d1,第二段是删除区称为d2,第三段是删除区之后的称为d3,
将d3的数据复制到 Vaule[start + len] 之后。(len的空间是为为替代的字符串空出来的)
将str的字符复制到value[start]之后
public String substring(int start, int end)
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);
}
1
www.cnblogs.com/sflhw/articles/12962624.html
www.cnblogs.com/sflhw/articles/12962610.html
www.cnblogs.com/sflhw/articles/12962597.html
www.cnblogs.com/sflhw/articles/12962584.html
www.cnblogs.com/sflhw/articles/12962571.html
www.cnblogs.com/sflhw/articles/12962559.html
www.cnblogs.com/sflhw/articles/12962551.html
www.cnblogs.com/sflhw/articles/12962543.html
www.cnblogs.com/sflhw/articles/12962538.html
www.cnblogs.com/sflhw/articles/12962534.html
www.cnblogs.com/sflhw/articles/12962734.html
www.cnblogs.com/sflhw/articles/12962729.html
www.cnblogs.com/sflhw/articles/12962717.html
www.cnblogs.com/sflhw/articles/12962704.html
www.cnblogs.com/sflhw/articles/12962691.html
www.cnblogs.com/sflhw/articles/12962683.html
www.cnblogs.com/sflhw/articles/12962670.html
www.cnblogs.com/sflhw/articles/12962658.html
www.cnblogs.com/sflhw/articles/12962647.html
www.cnblogs.com/sflhw/articles/12962637.html
www.cnblogs.com/sflhw/articles/12962818.html
www.cnblogs.com/sflhw/articles/12962805.html
www.cnblogs.com/sflhw/articles/12962792.html
www.cnblogs.com/sflhw/articles/12962784.html
www.cnblogs.com/sflhw/articles/12962772.html
www.cnblogs.com/sflhw/articles/12962763.html
www.cnblogs.com/sflhw/articles/12962754.html
www.cnblogs.com/sflhw/articles/12962749.html
www.cnblogs.com/sflhw/articles/12962744.html
www.cnblogs.com/sflhw/articles/12962739.html
www.cnblogs.com/cutman/articles/12962618.html
www.cnblogs.com/cutman/articles/12962609.html
www.cnblogs.com/cutman/articles/12962596.html
www.cnblogs.com/cutman/articles/12962583.html
www.cnblogs.com/cutman/articles/12962569.html
www.cnblogs.com/cutman/articles/12962558.html
www.cnblogs.com/cutman/articles/12962549.html
www.cnblogs.com/cutman/articles/12962541.html
www.cnblogs.com/cutman/articles/12962536.html
www.cnblogs.com/cutman/articles/12962532.html
www.cnblogs.com/cutman/articles/12962732.html
www.cnblogs.com/cutman/articles/12962724.html
www.cnblogs.com/cutman/articles/12962716.html
www.cnblogs.com/cutman/articles/12962703.html
www.cnblogs.com/cutman/articles/12962690.html
www.cnblogs.com/cutman/articles/12962677.html
www.cnblogs.com/cutman/articles/12962665.html
www.cnblogs.com/cutman/articles/12962657.html
www.cnblogs.com/cutman/articles/12962644.html
www.cnblogs.com/cutman/articles/12962631.html
www.cnblogs.com/cutman/articles/12962813.html
www.cnblogs.com/cutman/articles/12962806.html
www.cnblogs.com/cutman/articles/12962794.html
www.cnblogs.com/cutman/articles/12962786.html
www.cnblogs.com/cutman/articles/12962773.html
www.cnblogs.com/cutman/articles/12962760.html
www.cnblogs.com/cutman/articles/12962752.html
www.cnblogs.com/cutman/articles/12962747.html
www.cnblogs.com/cutman/articles/12962743.html
www.cnblogs.com/cutman/articles/12962738.html
www.cnblogs.com/wxy8/p/12962604.html
www.cnblogs.com/wxy8/p/12962591.html
www.cnblogs.com/wxy8/p/12962578.html
www.cnblogs.com/wxy8/p/12962570.html
www.cnblogs.com/wxy8/p/12962557.html
www.cnblogs.com/wxy8/p/12962550.html
www.cnblogs.com/wxy8/p/12962542.html
www.cnblogs.com/wxy8/p/12962539.html
www.cnblogs.com/wxy8/p/12962535.html
www.cnblogs.com/wxy8/p/12962531.html
www.cnblogs.com/wxy8/p/12962723.html
www.cnblogs.com/wxy8/p/12962710.html
www.cnblogs.com/wxy8/p/12962702.html
www.cnblogs.com/wxy8/p/12962689.html
www.cnblogs.com/wxy8/p/12962678.html
www.cnblogs.com/wxy8/p/12962664.html
www.cnblogs.com/wxy8/p/12962656.html
www.cnblogs.com/wxy8/p/12962638.html
www.cnblogs.com/wxy8/p/12962630.html
www.cnblogs.com/wxy8/p/12962617.html
www.cnblogs.com/wxy8/p/12962812.html
www.cnblogs.com/wxy8/p/12962804.html
www.cnblogs.com/wxy8/p/12962779.html
www.cnblogs.com/wxy8/p/12962766.html
www.cnblogs.com/wxy8/p/12962759.html
www.cnblogs.com/wxy8/p/12962751.html
www.cnblogs.com/wxy8/p/12962746.html
www.cnblogs.com/wxy8/p/12962742.html
www.cnblogs.com/wxy8/p/12962737.html
www.cnblogs.com/wxy8/p/12962731.html
www.cnblogs.com/lucongrui/articles/12962811.html
www.cnblogs.com/lucongrui/articles/12962799.html
www.cnblogs.com/lucongrui/articles/12962793.html
www.cnblogs.com/lucongrui/articles/12962785.html
www.cnblogs.com/lucongrui/articles/12962771.html
www.cnblogs.com/lucongrui/articles/12962753.html
www.cnblogs.com/lucongrui/articles/12962748.html
www.cnblogs.com/lucongrui/articles/12962745.html
www.cnblogs.com/lucongrui/articles/12962740.html
www.cnblogs.com/lucongrui/articles/12962730.html
www.cnblogs.com/lucongrui/articles/12962722.html
www.cnblogs.com/lucongrui/articles/12962709.html
www.cnblogs.com/lucongrui/articles/12962700.html
www.cnblogs.com/lucongrui/articles/12962688.html
www.cnblogs.com/lucongrui/articles/12962676.html
www.cnblogs.com/lucongrui/articles/12962663.html
www.cnblogs.com/lucongrui/articles/12962650.html
www.cnblogs.com/lucongrui/articles/12962643.html
www.cnblogs.com/lucongrui/articles/12962632.html
www.cnblogs.com/lucongrui/articles/12962625.html
www.cnblogs.com/lucongrui/articles/12962611.html
www.cnblogs.com/lucongrui/articles/12962603.html
www.cnblogs.com/lucongrui/articles/12962590.html
www.cnblogs.com/lucongrui/articles/12962577.html
www.cnblogs.com/lucongrui/articles/12962564.html
www.cnblogs.com/lucongrui/articles/12962552.html
www.cnblogs.com/lucongrui/articles/12962544.html
No comments:
Post a Comment