top

關於ensureCapacity() 及setLength() 的問題

想請教一下關於ensureCapacity() 及setLength() 的作用, 在網上找到一些說明,可是都不是太懂,另外還有這2個CODE在以下這段CODE的作用,
因為在網上都是看到有關array的,可是下面的都沒有用到array,所以想了解一下當中的用處和分別,謝謝各位高手!!!

public static void main(String[]args){
StringBuffer buf;
buf = new StringBuffer("Hello World");

System.out.println("buf = \"" + buf + "\"");
System.out.println("length = " +buf.length());
System.out.println("capacity = " + buf.capacity());

buf.ensureCapacity(50);   
System.out.println("New capacity = " + buf.capacity());

buf.setLength(5);
System.out.println("buf = \"" + buf + "\"");
System.out.println("length = " +buf.length());
System.out.println("capacity = " + buf.capacity());
}