自创一个StringBuffer类

定义一个MyStringBuffer类
public class MyStringBuffer implements IStringBuffer{
int length = 0; //定义字符串长度
int capacity = 19; //定义SB类默认容量为19
char []value ;

public MyStringBuffer()
{
	value = new char[capacity];
}
public MyStringBuffer(String str)
{
	
	if (null==str)	return;
	//因为构造器不能重复调用,所以必须把str长度超过SB容积的情况放在前面
	if(str.length()>capacity)
	{
		capacity = length*2;
		value = new char[capacity];  //如果SB的容量小于接收的字符串,则将容量改为接收字符串的两倍
		
	}
	if(str.length()<=capacity)
	{
		value = new char[capacity];																		
		System.arraycopy(str.toCharArray(), 0, value, 0, str.length());		//将字符串复制到value数组中
		//System.arraycopy(src(被复制的数组), srcPos(复制到的结果数组), dest, destPos, length);
	}
	length = str.length();		//记录数组value即字符串s的长度

	
}
@Override
public void append(String str) {
	// TODO Auto-generated method stub
	insert(length,str);
}

@Override
public void append(char c) {
	// TODO Auto-generated method stub
	insert(length,String.valueOf(c));
}

@Override
public void insert(int pos, char b) {
	// TODO Auto-generated method stub
	insert(pos,String.valueOf(b));
}

@Override
public void insert(int pos, String b) {
	// TODO Auto-generated method stub
	// 边界情况的判断
	if(pos<0) return;
	if(pos>b.length()) return;
	if(null==b) return;
	
	//扩容操作
	while(b.length()+length>capacity)
	{
		capacity = (b.length()+length)*2;		//将容量扩展为value与传入字符串长度之和的2倍(倍数任意)
		char []nvalue = new char[capacity];		//创建一个新的较长字符数组nvalue
		System.arraycopy(value, 0, nvalue, 0, value.length);		//将value中的值复制到较长的数组nvalue中去
		value = nvalue;				//使value指向nvalue
	}
	
	//插入操作
	char []bts = b.toCharArray();		//将传入字符串转化为字符数组
	//原元素后移
	System.arraycopy(value, pos, value, pos+bts.length, length-pos); 	// abcdef->要在cd之间插入xyz->abcdefdef->abcxyzdef
	//pos代表d的位置,为3(数组下标),移动之后可以看到最后下标为6,所以从d开始每个数字都移动了3个单位,即b.length
	//复制的元素有defg共4个,所以为length-pos = 7-3
	
	//开始插入
	System.arraycopy(bts, 0, value, pos, bts.length); //将传入字符串的字符数组插入到指定位置Pos中去
	length = length + bts.length;		//更新length
	
}

@Override
public void delete(int start) {
	// TODO Auto-generated method stub
	delete(start,length);
}

@Override
public void delete(int start, int end) {

// delete(int a,int b)有两个参数,
// 使用时删除索引从a开始(包含a)到b(不包含b)的所有字符;
// TODO Auto-generated method stub
// char []da = new char[length];
// System.arraycopy(value, end, da, 0, length-end);
// System.arraycopy(da, 0, value, start, da.length);
// char []nvalue2 = new char [length-(end-start)];
// System.arraycopy(value, 0, nvalue2, 0, nvalue2.length);
// value = nvalue2;
// length = value.length;
//标准答案:

	//首先判断边界条件
	if(start<0) return;
	if(end>length) return;
	if(start>length) return;
	if(start>=end) return;
	
	System.arraycopy(value, end, value, start, length-end);
	length = length-(end-start);
	
}

@Override
public void reverse() {
	// TODO Auto-generated method stub
	for(int i=0;i<length/2;i++)
	{
		char temp = value[i];
		value[i] = value[length-i-1];	//	length = 5:[0,1,2,3,4] 对应:  0-4 1-3 2-2
		value[length-i-1] = temp;
	}
}

@Override
public int length() {
	// TODO Auto-generated method stub
	return length;
}
public String toString()
{
	char realvalue[] = new char[length];		//字符数组的容量并不一定都会用完,用realvalue来放value中具有值的单位,而省略掉为空的单位
	System.arraycopy(value, 0, realvalue, 0, length);		//将不为空的数组元素复制到新数组中

// String reals = String.copyValueOf(realvalue);
return new String(realvalue); //初始化字符串并返回
}
public static void main(String[] args) {

	//自己开发的功能要测试边界点比如插入删除要对0、length进行操作
	var s = new MyStringBuffer("Hello");	
	var s1 = new MyStringBuffer("abcdefg");
	s.insert(s.length, " world!a");
	s.insert(1, "a");
	
	System.out.println(s);
	s1.delete(0,s1.length()); //左闭右开
	System.out.println(s1);

}

实现的接口为IStringBuffer:

public interface IStringBuffer {
public void append(String str); //追加字符串
public void append(char c); //追加字符
public void insert(int pos,char b); //指定位置插入字符
public void insert(int pos,String b); //指定位置插入字符串
public void delete(int start); //从开始位置删除剩下的
public void delete(int start,int end); //从开始位置删除结束位置-1
public void reverse(); //反转
public int length(); //返回长度
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pascalzhli

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值