1. 程式人生 > >JavaSE8基礎 StringBuffer delete trimToSize 清空字符串緩沖區與整理緩沖區的空間

JavaSE8基礎 StringBuffer delete trimToSize 清空字符串緩沖區與整理緩沖區的空間

ase true 空字符 ide rim alt capacity stat app



os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)


code:

package jizuiku1;

public class Demo100 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		sb.append("cnblog");
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
		
		System.out.println("------------------");
		
		sb.append("jizuiku jizuiku");
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
		
		System.out.println("------------------");		
		
		sb.delete(0, sb.length()); //區間表示[0,sb.length)
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
		System.out.println("------------------");
		
		sb.append("cnblog jizuiku jizuiku");
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
		
		System.out.println("------------------");		
		
		sb.delete(0, sb.length()).trimToSize(); //區間表示[0,sb.length)
												//trimToSize 整理一下空間
		System.out.println(sb);
		System.out.println(sb.length());
		System.out.println(sb.capacity());
	}
}


result:
技術分享



Java優秀,值得學習。
學習資源:API手冊+Java源碼+清凈的心地。

JavaSE8基礎 StringBuffer delete trimToSize 清空字符串緩沖區與整理緩沖區的空間