dp_sp不是缩写是2b倒写切记
StringBuffer sb = new StringBuffer("abcd");
StringBuffer buffer1 = new StringBuffer(10);
StringBuffer buffer2= new StringBuffer(3);
StringBuffer buffer3= new StringBuffer(1);
buffer1.append("abcd");
buffer2.append("abcd");//扩容的长度为原来的2倍+2
buffer3.append("abcd");//2*1<4 扩容长度为4
System.out.println(buffer1.length()+" && "+buffer2.length() +" && "+buffer3.length() +" && "+sb.length());
System.out.println(buffer1.capacity()+" && "+buffer2.capacity()+" && "+buffer3.length() +" && "+sb.capacity());
输出结果: 4 && 4 && 4 && 4 10 && 8 && 4 && 20
public class Demo {
public static void main(String [] args){
StringBuffer sb1 = new StringBuffer(10);//构造一个不带字符,但具有指定初始容量的字符串缓冲区。
System.out.println(sb1.length());//0
System.out.println(sb1.capacity());//10,指定了长度
System.out.println("============");
sb1.append("abcd");
System.out.println(sb1.length());//4
System.out.println(sb1.capacity());//10
System.out.println("============");
sb1.append("efghij");
System.out.println(sb1.length());//10
System.out.println(sb1.capacity());//10
System.out.println("============");
sb1.append("k");
System.out.println(sb1.length());//11
System.out.println(sb1.capacity());//22,当length>capacity时,capacity=(原capacity+1)*2
System.out.println("============");
sb1.append("abcdefghijk");
System.out.println(sb1.length());//22
System.out.println(sb1.capacity());//22,length==capacity
System.out.println("============");
sb1.append("abcdefghijkabcdefghijkabcdefghijkabcdefghijk");//相当于6段"abcdefghijk"
System.out.println(sb1.length());//66
System.out.println(sb1.capacity());//66,(22+1)*2=46,仍小于66。干脆和length同步
System.out.println("====================================================================");
System.out.println("============");
StringBuffer sb2 = new StringBuffer();//构造一个其中不带字符的字符串缓冲区,其初始容量为 16
sb2.append("hello");
System.out.println(sb2.length());//5
System.out.println(sb2.capacity());//16
System.out.println("============");
sb2.append("6789abcdefg");
System.out.println(sb2.length());//16
System.out.println(sb2.capacity());//16,length==capacity
System.out.println("============");
sb2.append("h");
System.out.println(sb2.length());//17
System.out.println(sb2.capacity());//34,当length>capacity,capacity=(原capacity+1)*2
System.out.println("============");
sb2.append("hello6789abcdefghhello6789abcdefghhello6789abcdefghhello6789abcdefgh");
System.out.println(sb2.length());//17*5=85 复制5次“hello6789abcdefgh”
System.out.println(sb2.capacity());//85,(34+1)*2=70,即使加1倍增,仍无法满足,干脆直接和length同步
System.out.println("==========================================================");
StringBuffer sb3 = new StringBuffer("hello");//构造一个字符串缓冲区,它包含与指定的字符。该字符串缓冲区的初始容量为 16加上参数的长度。
System.out.println(sb3.length());//5
System.out.println(sb3.capacity());//StringBuffer的的初始大小为(16+初始字符串长度);5+16=21
System.out.println("============");
sb3.append("6789abcdefghijkL");
System.out.println(sb3.length());//21
System.out.println(sb3.capacity());//21
System.out.println("============");
sb3.append("M");
System.out.println(sb3.length());//22
System.out.println(sb3.capacity());//44,(原capacity+1)*2
System.out.println("============");
sb3.append("hello6789abcdefghijkLMhello6789abcdefghijkLMhello6789abcdefghijkLMhello6789abcdefghijkLM");//相当于5段“hello6789abcdefghijkLM”
System.out.println(sb3.length());//110
System.out.println(sb3.capacity());//110,与length同步了;
}
}
StringBuffer s1=new StringBuffer(10);//初始化容量为10(默认为16) s1.append(“1234”)//添加字符串1234,长度不超过初始化容量,length为4,capacity为10 public int length()返回长度(字符数)。指定者: 接口CharSequence中的length返回: 此对象表示的当前字符序列的长度。
public int capacity()
public int length()返回长度(字符数)。
public int capacity()返回当前容量。容量指可用于最新插入的字符的存储量,超过这一容量就需要再次进行分配。