仿照StringBuffer,根据接口IStringBuffer ,自己做一个MyStringBuffer。
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 delect(int start);//从开始位置删除剩下的
public void delect(int start,int end);//从开始位置删除到结束位置-1
public void reverse();//反转
public int length();//返回长度
}
MyStringBuffer实现类:
public class MyStringBuffer implements IStringBuffer {
int capacity = 16;
int length = 0;
char[] value;
public MyStringBuffer() {
value = new char[capacity];
}
// 有参构造方法
public MyStringBuffer(String str) {
this();
if(null == str) {
return;
}
if (capacity < str.length()) {
capacity = value.length * 2;
value = new char[capacity];
}
if(capacity >= str.length()) {
System.arraycopy(str.toCharArray(), 0, value, 0, str.length());
length = str.length();
}
}
public void append(String str) {
insert(length, str);
}
public void append(char c) {
append(String.valueOf(c));
}
public void insert(int pos, char b) {
insert(pos, String.valueOf(b));
}
public void delete(int start) {
delete(start, length);
}
public void delete(int start, int end) {
//边界条件判断
if (start < 0) {
return;
}
if (start > length) {
return;
}
if (end < 0) {
return;
}
if (end > length) {
return;
}
if (start >= end) {
return;
}
System.arraycopy(value, end, value, start, length - end);
length -= end - start;
}
public void reverse() {
for(int i = 0; i < length / 2; i++) {
char temp = value[i];
value[i] = value[length - i -1];
value[length - i - 1] = temp;
}
}
public int length() {
// TODO Auto-generated method stub
return length;
}
public void insert(int pos, String b) {
// 边界条件判断
if(pos < 0) {
return;
}
if(pos > length) {
return;
}
if(b == null) {
return;
}
// 扩容
while(length + b.length() > capacity) {
capacity = (int) ((length + b.length()) * 1.5f);
char[] newValue = new char[capacity];
System.arraycopy(value, 0, newValue, 0, length);
value = newValue;
}
char[] cs = b.toCharArray();
// 先把已经存在的数据往后移
System.arraycopy(value, pos, value, pos + cs.length, length - pos);
// 把要插入的数据插入到指定位置
System.arraycopy(cs, 0, value, pos, cs.length);
length = length + cs.length;
}
public String toString() {
char[] realValue = new char[length];
System.arraycopy(value, 0, realValue, 0, length);
return new String(realValue);
}
public static void main(String[] args) {
MyStringBuffer sb = new MyStringBuffer("there light");
System.out.println(sb);
sb.insert(0, "let ");
System.out.println(sb);
sb.insert(10, "be ");
System.out.println(sb);
sb.insert(0, "God Say:");
System.out.println(sb);
sb.append("!");
System.out.println(sb);
sb.append('?');
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.delete(0, 4);
System.out.println(sb);
sb.delete(4);
System.out.println(sb);
}
}
性能比较
使用 Java 自带的 StringBuffer 和 自己开发的 MyStringBuffer 性能比较。参考比较方案:
- 1. 生成长度是10的随机字符串
- 2. 使用StringBuffer追加1000000次统计时间
- 3. 使用MyStringBuffer追加1000000次统计时间
测试代码:
public class TestString {
public static void main(String[] args) {
int total = 5000000;
String s = randomString(10);
MyStringBuffer msb = new MyStringBuffer();
StringBuffer sb = new StringBuffer();
long start = System.currentTimeMillis();
for (int i = 0; i <total; i++) {
sb.append(s); //使用 StringBuffer
}
long end = System.currentTimeMillis();
System.out.printf("使用StringBuffer的方式,连接%d次,耗时%d毫秒%n",total,end-start);
start = System.currentTimeMillis();
for (int i = 0; i <total; i++) {
msb.append(s); //使用MyStringBuffer
}
end = System.currentTimeMillis();
System.out.printf("使用MyStringBuffer的方式,连接%d次,耗时%d毫秒%n",total,end-start);
}
private static String randomString(int length) {
String pool = "";
for (short i = '0'; i <= '9'; i++) {
pool += (char) i;
}
for (short i = 'a'; i <= 'z'; i++) {
pool += (char) i;
}
for (short i = 'A'; i <= 'Z'; i++) {
pool += (char) i;
}
char cs[] = new char[length];
for (int i = 0; i < cs.length; i++) {
int index = (int) (Math.random() * pool.length());
cs[i] = pool.charAt(index);
}
String result = new String(cs);
return result;
}
}
输出结果:
使用StringBuffer的方式,连接5000000次,耗时357毫秒
使用MyStringBuffer的方式,连接5000000次,耗时302毫秒
主要参考:http://how2j.cn/k/number-string/number-string-mystringbuilder/331.html#nowhere