定义一个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(); //返回长度
}