数据结构变量字符串类(JAVA)
package Chapter3;
import java.io.Serializable;
public class MyStringBuffer implements Serializable {
private char[] value; //字符数组,私有成员变量
private int n; //串长度
public MyStringBuffer(int capacity){
this.value=new char[capacity];
this.n=0;
}
public MyStringBuffer(){
this(16);
}
public MyStringBuffer(String s){
this(s.length()+16);
this.n=s.length();
for (int i=0; i this.n){
i=this.n;
}
return this.value[i];
}
public void setCharAt(int i, char ch){
if (i < 0){
i=0;
}
else if (i > this.n){
i=this.n;
}
this.value[i]=ch;
}
public synchronized MyStringBuffer insert(int i, String s){
if (this.n==0 && i==0 || this.n>0 && i>=0 && i<=this.n){
if (s==null){
s="";
}
char[] temp=this.value;
//若数组空间不足,则扩充
if (this.value.length =i; j--){
this.value[j + s.length()]=temp[j];
}
for (int j=0; j=0 && begin=0 && begin<=end){
if (end > this.n){
end=this.n;
}
for (int i=0; i