public static void main(String args[]){
StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());
sb.append("abcabcabcabcabcabcab");
System.out.println(sb.length());
System.out.println(sb.capacity());
System.out.println("-----");
sb.append("abcabcabcabcabcabcab");
System.out.println(sb.length());
System.out.println(sb.capacity());
System.out.println("-----");
sb.trimToSize();
System.out.println(sb.capacity());
System.out.println(sb.length());
}
o/p istStringbuffer Kapazität()
16
20
34
-----
40
70
-----
40
40
Meine Frage, die in der zweiten Zeile ist, sollte die Kapazität 36 (20 + 16) ist. warum zeigt es als 34.
Ihr Titel und Tags sagen * StringBuffer *. Dein Code sagt * StringBuilder *. Welches ist es? –
Related: (wenn Sie meinten StringBuffer) http://StackOverflow.com/Questions/8011574/what-is-the-capacity-of-a-stringbuffer?rq=1 – Thilo