标签:java算法
MyStringBuffer:对象不变,里面的值改变,即字符数组改变,这是与MyString类最大的差别。
//用户自定义的MyStringBuffer类 public class MyStringBuffer { private char[] value; //字符数组 private int count; //字符串的长度 //从源串指定下标开始拷贝指定长度的字符串到目标串指定为下标开始 //char[] src:源串 //int srcPos:源串的开始下标 //char[] dst:目标串 //int dstPos:目标串开始下标 //int length:拷贝长度 public static void arrayCopy(char[] src,int srcPos,char[] dst,int dstPos,int length) { //如果源串起始下标+拷贝长度>源串长度或者目标串起始下标+拷贝长度>目标串长度 if(srcPos+length>src.length||dstPos+length>dst.length) { throw new StringIndexOutOfBoundsException(length); } for(int i=0;i<length;i++) { dst[dstPos++]=src[srcPos++]; } } //创建新的空间,将远value的数组放入新的空间中,并让value指向新的空间 //这个是扩充字符数组的空间 private void expandCapacity(int newCapacity) { char newValue[] = new char[newCapacity]; arrayCopy(value,0,newValue,0,count); value=newValue; //让value指向新创建的newValue数组。 //count=newCapacity; } public MyStringBuffer(String str) { char[] chararray = str.toCharArray(); value=chararray; count=chararray.length; } public MyStringBuffer concat(MyStringBuffer str) { int otherLen = str.length(); if(otherLen==0) { return this; } this.expandCapacity(count+otherLen); System.out.println("src.lenght="+str.length()); System.out.println("des.lenght="+this.length()); arrayCopy(str.toCharArray(),0,this.toCharArray(),this.length(),str.length()); count+=otherLen; return this; } public int length() { return count; } @Override public String toString() { // TODO Auto-generated method stub String str=""; for(int i=0;i<count;i++) { str+=value[i]; } return str; } //这是直接返回里面的值,因为StringBuffer内的值是随时改变的 public char[] toCharArray() { return value; } }
总结::
StringBuffer是对象没有改变,但是对象里的值变了。
这里与String不同的是,每改变String,都要new出一个新的String。
标签:java算法
原文地址:http://wukong0716.blog.51cto.com/10442773/1687539