StringBuilder和Integer常用的函数具体的实现步骤
StringBuilder常用的函数具体的实现步骤
1.append
public AbstractStringBuilder append(String str) {
//判断是该字符串是否为空
if (str == null)
return appendNull();
//计算str字符串长度
int len = str.length();
//扩充当前字符串的容量
ensureCapacityInternal(count + len);
//把值依次插入到当前对象有末端
str.getChars(0, len, value, count);
//更新当前字符串的容量容量
count += len;
//返回当前对象
return this;
}
2.capacity
public int capacity() {
//返回当前Value长度
return value.length;
}
3.length
public int length() {
//返回当前value的有效数
return count;
}
4. charAt
//重写的函数
@Override
public char charAt(int index) {
//如过index<或者index大于等于此时value的容量进行异常处理
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
//返回value在index的值
return value[index];
}
5.deleteCharAt
public AbstractStringBuilder deleteCharAt(int index) {
//如果index<或者index大于等于此时value的容量进行异常处理
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
// 将value的index处的元素删除
System.arraycopy(value, index+1, value, index, count-index-1);
//value的容量更新
count--;
//返回当前的对象
return this;
}
6.delete
public AbstractStringBuilder delete(int start, int end) {
//如果start<0,进行异常处理
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
//如果end>count,令end等于此时的容量
if (end > count)
end = count;
//如果start>end,进行异常处理
if (start > end)
throw new StringIndexOutOfBoundsException();
//计算起始到结束之间的差值
int len = end - start;
//如果这个值>0,删除value在这个范围的数
if (len > 0) {
System.arraycopy(value, start+len, value, start, count-end);
//更新value的容量
count -= len;
}
//犯规当前的对象
return this;
}
7. indexOf
static int indexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex) {
//返回此时给的如字符串,第一个字符在value中的位置
return indexOf(source, sourceOffset, sourceCount,
target.value, 0, target.value.length,
fromIndex);
}
8.insert
public AbstractStringBuilder insert(int offset, String str) {
//判断如果成立则进行异常处理
if ((offset < 0) || (offset > length()))
throw new StringIndexOutOfBoundsException(offset);
if (str == null)
str = "null";
//当前str的容量
int len = str.length();
//扩充容量
ensureCapacityInternal(count + len);
//在value中添加str
System.arraycopy(value, offset, value, offset + len, count - offset);
//把值依次插入到当前对象有末端
str.getChars(value, offset);
//更新value的容量
count += len;
//返回当前对象
return this;
}
Integer常用的函数具体的实现步骤
1.compareTo
public int compareTo(Integer anotherInteger) {
//对两个对象中int值进行比较
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
//x < y返回-1,x == y返回0,x》yf返回1
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
2.intValue
public int intValue() {
返回当前 value
return value;
}
3.equals
public boolean equals(Object obj) {
//先判断此时 obj是否是Integer类型的,是进行对比地址,不是返回false
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
4.toHexString,toOctalString,toBinaryString,toString
//都调用了toUnsignedString0函数
public static String toHexString(int i) {
return toUnsignedString0(i, 4);
}
public static String toOctalString(int i) {
return toUnsignedString0(i, 3);
}
public static String toBinaryString(int i) {
return toUnsignedString0(i, 1);
}
toUnsignedString0方法是先计算转换成对应进制需要的字符数,也就是得到chars,然后再通过 formatUnsignedInt 方法来填充字符数组得到结果。最复杂的就在于这个 formatUnsignedInt 方法。
private static String toUnsignedString0(int val, int shift) {
// assert shift > 0 && shift <=5 : "Illegal shift value";
int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
int chars = Math.max(((mag + (shift - 1)) / shift), 1);
char[] buf = new char[chars];
formatUnsignedInt(val, shift, buf, 0, chars);
// Use special constructor which takes over "buf".
return new String(buf, true);
}
static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
int charPos = len;
/*
如果是二进制,因为2的1次方是2,所以shift就是1,那么radix就是1左移1位得到2,mask就是1,对应的二进制就是1;
*/
int radix = 1 << shift;
int mask = radix - 1;
/*
循环val & mask的过程就像分组转换的过程,每次循环可以将mask对应的位数得到,再利用digits数组转换成对应的字符,这个字符就是这次分组的这几位所对应的结果,每次分组得到结果以后把val右移相应的位数,继续下一轮的循环分组。
还是以19转成八进制举例,看一下程序是怎么走的:
val为19,shift为3,radix为8,mask为7。
第一次循环:19 & 7就是10011 & 111,结果为11,也就是3,通过digits数组得到这一位字符为3。然后10011右移3位,得到val为10。
第二次循环:10 & 111,结果为10,也就是2,通过digits数组得到这一位字符为2。然后10右移3位,得到val为0,循环结束。
最终结果就是23。
*/
do {
buf[offset + --charPos] = Integer.digits[val & mask];
val >>>= shift;
} while (val != 0 && charPos > 0);
return charPos;
}
5.valueOf
public static Integer valueOf(int i) {
/* 判断 i 值是否在-128和127之间,
如果是的话,会从IntegerCache的cache(其实是一个用 final static
修饰的数组) 中返回 Integer 对象,如果不在这个范围内,会创建一个新的
Integer 对象。
*/
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}