Own development of a Java StringBuffer
The interface IStringBuffer, do yourself a MyStringBuffer
Step. 1: IStringBuffer Interface
package character;
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(); //返回长度
}
Step 2: value and capacity
value: storing a character array
capacity: Capacity
constructor with no arguments: the initialization value according to the capacity
public MyStringBuffer(){
value = new char[capacity];
}
package character;
public class MyStringBuffer implements IStringBuffer{
int capacity = 16;
int length = 0;
char[] value;
public MyStringBuffer(){
value = new char[capacity];
}
@Override
public void append(String str) {
// TODO Auto-generated method stub
}
@Override
public void append(char c) {
// TODO Auto-generated method stub
}
@Override
public void insert(int pos, char b) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start, int end) {
// TODO Auto-generated method stub
}
@Override
public void reverse() {
// TODO Auto-generated method stub
}
@Override
public int length() {
// TODO Auto-generated method stub
return 0;
}
}
Step 3: parameterized constructor
package character;
public class MyStringBuffer implements IStringBuffer{
int capacity = 16;
int length = 0;
char[] value;
public MyStringBuffer(){
value = new char[capacity];
}
//有参构造方法
public MyStringBuffer(String str){
if(null!=str)
value =str.toCharArray();
length = value.length;
if(capacity<value.length)
capacity = value.length*2;
}
@Override
public void append(String str) {
// TODO Auto-generated method stub
}
@Override
public void append(char c) {
// TODO Auto-generated method stub
}
@Override
public void insert(int pos, char b) {
}
@Override
public void delete(int start) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start, int end) {
// TODO Auto-generated method stub
}
@Override
public void reverse() {
// TODO Auto-generated method stub
}
@Override
public int length() {
// TODO Auto-generated method stub
return length;
}
@Override
public void insert(int pos, String b) {
}
}
Step 4: Reverse reverse
package character;
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();
}
@Override
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;
}
}
@Override
public void append(String str) {
// TODO Auto-generated method stub
}
@Override
public void append(char c) {
// TODO Auto-generated method stub
}
@Override
public void insert(int pos, char b) {
// TODO Auto-generated method stub
}
@Override
public void insert(int pos, String b) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start, int end) {
// TODO Auto-generated method stub
}
@Override
public int length() {
// TODO Auto-generated method stub
return 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");
sb.reverse();
System.out.println(sb);
}
}
Step 5: inserting insert and append
Determine the boundary conditions
prior to insertion, we must first determine that some of the boundary conditions. For example, the insertion position is legitimate, whether the inserted string is empty
Expansion
- To determine whether additional capacity is needed. If the inserted string with existing content length exceeds the total capacity, then the capacity is needed.
- Length of the array is fixed and can not be changed, the array itself does not support expansion. We use alternative way to solve this problem.
- The length of the string to be inserted and the already existing content, to calculate a new capacity. Then according to this capacity, create a new array, then the contents of the original array, copied to the new array to. And let the value of this reference point to the new array, so as to achieve the effect of expansion.
Insert string
- To find the location of the insertion of the string, starting from this position, the original data as two sections , the second half to move rearwardly a distance, this distance is exactly the length of the string is inserted
- Then the data to be inserted, insert this move out, just in a good position.
Modify the value of length of
the last length of the modified value, the original value plus the length of the string is inserted
insert (int, char)
parameter is the character of the insert method, by calling the insert (int, String) also achieved.
append
append, is inserted at the last position. It is not necessary to develop individual methods, direct insert method is called, you can achieve the effect of the final position of the insert
package character;
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();
}
@Override
public void append(String str) {
insert(length,str);
}
@Override
public void append(char c) {
append(String.valueOf(c));
}
@Override
public void insert(int pos, char b) {
insert(pos,String.valueOf(b));
}
@Override
public void delete(int start) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start, int end) {
// TODO Auto-generated method stub
}
@Override
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;
}
}
@Override
public int length() {
// TODO Auto-generated method stub
return length;
}
@Override
public void insert(int pos, String b) {
//边界条件判断
if(pos<0)
return;
if(pos>length)
return;
if(null==b)
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);
}
}
Step 6: Delete delete
package character;
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();
}
@Override
public void append(String str) {
insert(length,str);
}
@Override
public void append(char c) {
append(String.valueOf(c));
}
@Override
public void insert(int pos, char b) {
insert(pos,String.valueOf(b));
}
@Override
public void delete(int start) {
delete(start,length);
}
@Override
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;
}
@Override
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;
}
}
@Override
public int length() {
// TODO Auto-generated method stub
return length;
}
@Override
public void insert(int pos, String b) {
//边界条件判断
if(pos<0)
return;
if(pos>length)
return;
if(null==b)
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);
}
}
Exercise : performance comparison
Use built-in Java StringBuffer and the development of our own MyStringBuffer performance comparison.
Reference Comparison Program:
- Generating a random string of length 10
- Use StringBuffer time statistics appended 1000000
- Use MyStringBuffer time statistics appended 1000000