在Java中,StringBuffer是一个可变的字符串类,它允许对字符串进行动态修改。与String类不同的是,StringBuffer对象的长度和内容都可以被改变。这使得StringBuffer在需要频繁修改字符串内容时比String更加高效。
StringBuffer类提供了许多方法来操作字符串,比如添加字符、插入字符、替换字符、反转字符串等。由于StringBuffer是可变的,所以它的性能比直接对String对象进行字符串拼接要好,尤其是当需要频繁地修改字符串时。
另外,需要注意的是,StringBuffer是线程安全的,因此适合在多线程环境下使用。如果不需要线程安全性,则可以使用StringBuilder类,它与StringBuffer类功能类似,但不是线程安全的。
自定义MyStringBuffer实现了增加append,插入Insert,删除delete,自定义异常,自动扩容,字符串反转等等。
接口:
在Java中,接口(Interface)是一种抽象类型,它只包含方法的声明而没有方法的实现。接口定义了一组方法的签名,但具体的实现由实现接口的类来完成。任何实现了接口的类都必须实现接口中定义的所有方法。
接口在Java中有以下好处:
实现多继承:Java中一个类只能继承一个类,但可以实现多个接口。通过接口,可以让一个类具备多个行为,实现更灵活的设计。
规范和约定:接口定义了一组方法的规范,强制实现类提供这些方法的实现。这有助于代码的规范化和约定性,提高代码的可读性和可维护性。
解耦合:接口将接口与实现分离,降低了类之间的耦合度。通过接口,可以让类只依赖于接口而不依赖于具体的实现,提高了代码的灵活性和可复用性。
扩展性:接口可以在不影响已有代码的情况下扩展新的功能。实现类可以根据需要实现新的接口,而不需要修改现有代码。
package Basic.String_Num;public interface IStringBuffer {public void append(String str) throws MyStringBuffer.indexIsOutofRangeException,MyStringBuffer.indexIsNagetiveException;public void append(char c) throws MyStringBuffer.indexIsOutofRangeException,MyStringBuffer.indexIsNagetiveException;public void insert(int pos,char c) throws MyStringBuffer.indexIsOutofRangeException,MyStringBuffer.indexIsNagetiveException;public void insert(int pos,String str) throws MyStringBuffer.indexIsOutofRangeException, MyStringBuffer.indexIsNagetiveException;public void delete(int start) throws MyStringBuffer.indexIsOutofRangeException,MyStringBuffer.indexIsNagetiveException;public void delete(int start,int end) throws MyStringBuffer.indexIsOutofRangeException,MyStringBuffer.indexIsNagetiveException;public int length();public void reverse();
}
代码实现:
package Basic.String_Num;import javax.swing.plaf.synth.SynthDesktopIconUI;public class MyStringBuffer implements IStringBuffer{int length=0;int capacity=16;char []value;class indexIsNagetiveException extends Exception{public indexIsNagetiveException(){}public indexIsNagetiveException(String msg){super(msg);}}class indexIsOutofRangeException extends Exception{public indexIsOutofRangeException(){}public indexIsOutofRangeException(String msg){super(msg);}}public MyStringBuffer(){value=new char[capacity];}public MyStringBuffer(String str){this();if(str==null){throw new NullPointerException("空指针错误!");}if(str.length()>capacity){capacity=2*str.length();value=new char[capacity];}if(capacity>=str.length()){System.arraycopy(str.toCharArray(),0,value,0,str.length());length=str.length();}}@Overridepublic void append(String str) throws indexIsNagetiveException,indexIsOutofRangeException{insert(length,str);}@Overridepublic void append(char c) throws indexIsNagetiveException,indexIsOutofRangeException{append(String.valueOf(c));}@Overridepublic void insert(int pos, char c)throws indexIsNagetiveException,indexIsOutofRangeException {insert(pos,String.valueOf(c));}@Overridepublic void insert(int pos, String b) throws indexIsOutofRangeException, indexIsNagetiveException {//边界条件判断if(pos<0){throw new indexIsNagetiveException(String.valueOf(pos)+"下标为负异常");}if(pos>length){throw new indexIsOutofRangeException(String.valueOf(pos)+"下标超出范围异常");}if(b==null){throw new NullPointerException(String.valueOf(pos)+"空指针异常");}//扩容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=cs.length+length;}@Overridepublic void delete(int start) throws indexIsNagetiveException,indexIsOutofRangeException {delete(start,length);}@Overridepublic void delete(int start, int end) throws indexIsNagetiveException, indexIsOutofRangeException {if(start<0)throw new indexIsNagetiveException(String.valueOf(start)+"下标为负异常");if(start>length)throw new indexIsOutofRangeException(String.valueOf(start)+"下标超出范围");if(end<0)throw new indexIsNagetiveException(String.valueOf(end)+"下标为负异常");if(end>length)throw new indexIsOutofRangeException(String.valueOf(end)+"下标超出范围");if(start>=end)throw new indexIsOutofRangeException(String.valueOf(start)+"下标超出范围");System.arraycopy(value,end,value,start,length-end);length-=(end-start);}@Overridepublic int length() {return length;}@Overridepublic 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 String toString(){char[] realString=new char[length];System.arraycopy(value,0,realString,0,length);return new String(realString);}public static void main(String[] args) {String str = "dsjflabdji";MyStringBuffer sb = new MyStringBuffer(str);try {
// sb.insert(0,null);sb.insert(-1,"test");sb.insert(11,"test");sb.delete(-1, 1);sb.delete(6,5);sb.delete(11,0);} catch (Exception e) {// TODO: handle exceptionif(e instanceof NullPointerException)System.out.println("出现空指针异常");if(e instanceof indexIsNagetiveException | e instanceof indexIsOutofRangeException )System.out.println("出现下标异常");e.printStackTrace();}}}