可变数组:StringBuffer类(静态缓冲区)
提高字符串的操作效率
StringBuffer 底层实现原理是字符数组,没有final
StringBuffer,含有数组char[] value 默认长度是16
在JAVA中,数组是固定长度,一旦创建不能改变
StringBuffer 通过数组的赋值,实现数组的可变性,但数组本身不能改变长度
1.将任意数据类型,添加到缓冲区中append()
2.返回一个StringBuffer,因此可以使用方法调用链
3.将任意数据,插入到缓冲区的某一个索引上index(索引,插入内容)
4.缓冲区的删除方法:delete(int start, int end);deleteCharAt( int index);
5.修改缓冲区的单个字符setCharAt( int index ,char ch);
6.缓冲区字符串翻转,reverse()
7,缓冲区字符串截取,substring()返回的是String结构
8.将缓冲区变成不可变对象toString()
9.字符串对象与缓冲区对象互转
字符串变成缓冲区
String s = new String("asdf");
StringBuffer b = new StringBuffer();
b.append(s);缓冲区变成字符串
StringBuffer buffer = new StringBuffer("vbnm");
String string = buffer.toString();
注意返回值是String 要接收变量
/*
public final class StringBufferextends AbstractStringBuilderimplements java.io.Serializable, CharSequenceA cache of the last value returned by toString. Clearedwhenever the StringBuffer is modified.private transient char[] toStringCache;use serialVersionUID from JDK 1.0.2 for interoperability
static final long serialVersionUID = 3388685877147921107L;/*** Constructs a string buffer with no characters in it and an* initial capacity of 16 characters.public StringBuffer() {super(16);}/*** Constructs a string buffer with no characters in it and* the specified initial capacity.** @param capacity the initial capacity.* @exception NegativeArraySizeException if the {@code capacity}* argument is less than {@code 0}.public StringBuffer(int capacity) {super(capacity);}/*** Constructs a string buffer initialized to the contents of the* specified string. The initial capacity of the string buffer is* {@code 16} plus the length of the string argument.** @param str the initial contents of the buffer.public StringBuffer(String str) {super(str.length() + 16);append(str);}/*** Constructs a string buffer that contains the same characters* as the specified {@code CharSequence}. The initial capacity of* the string buffer is {@code 16} plus the length of the* {@code CharSequence} argument.* <p>* If the length of the specified {@code CharSequence} is* less than or equal to zero, then an empty buffer of capacity* {@code 16} is returned.** @param seq the sequence to copy.* @since 1.5public StringBuffer(CharSequence seq) {this(seq.length() + 16);append(seq);}*/import java.util.Scanner;public class test {public static void main(String[] args) {//System.out.println(getStringCont());//System.out.println(getString_1());//System.out.println(getString_2());//getString_3();//System.out.println(getString_4());System.out.println(getString_5());}//定义一个共能,从一个字符串中查找另一个字符串出现的次数public static int getStringCont() {Scanner sc1 = new Scanner(System.in);System.out.println("请输入一个字符串:");String s1 = sc1.nextLine();Scanner sc2 = new Scanner(System.in);System.out.println("请输入要查找的内容:");String s2 = sc2.nextLine();//定义计数器int count = 0;//定义下角标int index = 0;if (s1.length() == 0 || s2.length() == 0)return count;else {//判断s1第一次出现的位置,只要第一次出现的位置大于零,就进入循环while((index = s1.indexOf(s2))>0){count++;//新的字符串长度为查找内容后的下角标到结尾s1 = s1.substring(index+s2.length());}}return count;}public static StringBuffer getString_1(){StringBuffer buffer = new StringBuffer("abc");buffer.append("sdf");buffer.insert(2,"bnm");return buffer;}public static StringBuffer getString_2(){StringBuffer buffer_1 = new StringBuffer();buffer_1.append("poiu");System.out.println(buffer_1+"1");//删除指定下角标buffer_1.deleteCharAt(1);System.out.println(buffer_1+"2");//删除一堆buffer_1.delete(0,2);return buffer_1;}public static void getString_3(){StringBuffer buffer_2 = new StringBuffer();buffer_2.append("abcdefg");buffer_2.setCharAt(3,'p');System.out.println(buffer_2);}public static StringBuffer getString_4(){String s = "asdf";StringBuffer b = new StringBuffer();b.append(s);return b;}public static String getString_5(){StringBuffer buffer = new StringBuffer("vbnm");String string = buffer.toString();return string;}