Create a StringBuffer class

Define a MyStringBuffer class
public class MyStringBuffer implements IStringBuffer{ int length = 0; //Define the string length int capacity = 19; //Define the default capacity of the SB class as 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) has two parameters,
// delete all characters with index starting from a (including a) to b (not including b) when used;
// 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;
//standard answer:

	//首先判断边界条件
	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); //Initialize the string and return
}
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);

}

The implemented interface is IStringBuffer:

public interface IStringBuffer { public void append(String str); //Append string public void append(char c); //Append character public void insert(int pos,char b); //Insert character at specified position public void insert( int pos,String b); //Insert the string at the specified position public void delete(int start); //Delete the remaining public void delete(int start,int end); //Delete the end position from the start position -1 public void reverse(); //Reverse public int length(); //Return length )








Guess you like

Origin blog.csdn.net/weixin_45729934/article/details/105414644