Posted on 2012-02-29 11:01
齐纳尔多 阅读(319)
评论(0) 编辑 收藏 所属分类:
java
1. StringBuilder extends AbstractStringBuilder implements CharSequence, Serializable
(1)包含2个属性->char[] value(底层数据结构是数组), int count(字符串长度) 这2个属性在AbstractStringBuilder定义
比String少了一个offset属性
(2)构造 public StringBuilder(),调用父类构造, 默认构造一个大小为16的char[]数组
(3)append方法,和StringBuffer比较 不是线程安全的
public StringBuilder append(String str) {
super.append(str); //调用父类方法
return this; //返回当前对象引用,可以多次执行append, 构成一个链式调用
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;
}
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2; //扩大为(原来字符串的长度+要拼接的字符长度+1)的2倍
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity); //数组的copy, 重新构造一个新的数组,长度为newCapacity
}
//String方法
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > count) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, offset + srcBegin, dst, dstBegin,
srcEnd - srcBegin);
}
//重载
public AbstractStringBuilder append(CharSequence s, int start, int end) {
if (s == null)
s = "null";
if ((start < 0) || (end < 0) || (start > end) || (end > s.length())) //Assert
throw new IndexOutOfBoundsException(
"start " + start + ", end " + end + ", s.length() "
+ s.length());
int len = end - start;
if (len == 0)
return this;
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount); //扩容,分配一个新的数组,
for (int i=start; i<end; i++)
value[count++] = s.charAt(i);
count = newCount;
return this; //返回当前对象的引用,形成链式调用
}
}
(4)String源码中的重载方法
//从fromIndex位置处开始搜索
public int indexOf(int ch, int fromIndex) {
int max = offset + count; //字符串长度
char v[] = value; //备份一个数组
//校验判断
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= count) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
int i = offset + fromIndex;
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
//BMP--基本多文种平面(Basic Multilingual Plane)
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
for (; i < max ; i++) {
if (v[i] == ch) {
return i - offset;
}
}
return -1;
}
if (ch <= Character.MAX_CODE_POINT) {
//SMP-辅助平面(Supplementary Planes)
// handle supplementary characters here
//这里char[]长度为2因为char是2个字节, int(ch)> Character.MIN_SUPPLEMENTARY_CODE_POINT
char[] surrogates = Character.toChars(ch);
for (; i < max; i++) {
if (v[i] == surrogates[0]) {
if (i + 1 == max) {
break;
}
if (v[i+1] == surrogates[1]) {//如果第一个字符和第2个字符都相当的话,返回索引
return i - offset;
}
}
}
}
return -1;
}