StringBuffer源码浅析(insert方法)

一、insert,其实跟replace差不多,都是先通过System.arraycopy Method在字符数组value中预留一个空间,再用多一次这方法插入str或char[]。
1、insert(int index, char str[], int offset,int len)围观源码

// StringBuffer里面,调用父类的方法
public synchronized StringBuffer insert(int index, char str[], int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}

进入AbstractStringBuilder查看

public AbstractStringBuilder insert(int index, char str[], int offset,
int len)
{
// bounds checking
if ((index < 0) || (index > length()))
throw new StringIndexOutOfBoundsException(index);
// 注意:offset的值,offset + len不应大于str.length
if ((offset < 0) || (len < 0) || (offset > str.length - len))
throw new StringIndexOutOfBoundsException(
"offset " + offset + ", len " + len + ", str.length "
+ str.length);
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
// 从index开始,预留了长度为len的空间;从index+len开始,将长度为count-index的后半截补充。
System.arraycopy(value, index, value, index + len, count - index);
// str字符串数组,从索引offset开始复制,长度为len,复制到以索引index位置的value字符串数组。
System.arraycopy(str, offset, value, index, len);
count = newCount; //记得更新实际字符长度(字符数量)。
return this;
}


2、顺便看insert(int offset,String str) 对比 insert(int offset, Object obj)


// 将obj返回字符串形式,一样是调用父类insert(int offset,String str)
public synchronized StringBuffer insert(int offset, Object obj) {
super.insert(offset, String.valueOf(obj));
return this;
}

进入AbstractStringBuilder查看,跟第1点一样

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();
int newCount = count + len;
if (newCount > value.length)
expandCapacity(newCount);
System.arraycopy(value, offset, value, offset + len, count - offset);
str.getChars(value, offset); //getChars封装了System.arraycopy Method
count = newCount;
return this;
}

insert(int offset, char str[])操作一样不解释。

3、insert(int dstOffset, CharSequence s)

public StringBuffer insert(int dstOffset, CharSequence s) {
// Note, synchronization achieved via other invocations
if (s == null)
s = "null";
if (s instanceof String)
// 调用第2点,insert(int offset,String s)
return this.insert(dstOffset, (String)s);
// 看抽象类的方法定义
return this.insert(dstOffset, s, 0, s.length());
}

进入抽象类

public AbstractStringBuilder insert(int dstOffset, CharSequence s,
int start, int end) {
if (s == null)
s = "null";
if ((dstOffset < 0) || (dstOffset > this.length()))
throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
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);
// 以上大同小异,不说
// 预留空间
System.arraycopy(value, dstOffset, value, dstOffset + len,
count - dstOffset);
// start 到 end,逐个字符赋值到预留的空间上。
for (int i=start; i<end; i++)
value[dstOffset++] = s.charAt(i);
count = newCount;
return this;
}

:arrow:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值