Java源程序如下:
import java.io.*;
public class capacity
{
public static void main(String[] args)
{
StringBuffer buffer = new StringBuffer("Hello,how are you?");
String output= "buffer="+buffer.toString()+"\nlength="+buffer.length()+"\ncapacity="+buffer.capacity();
buffer.ensureCapacity(69);
output+= "\n\nNew capacity="+buffer.capacity();
buffer.setLength(10);
output+= "\n\nNew length="+buffer.length()+"\nbuffer="+buffer;
System.out.println(output);
}}
程序运行结果如下:
buffer=Hello,how are you?
length=18
capacity=34New capacity=70New length=10
buffer=Hello,how
其中length=18,New length=10明白,capacity=34和New capacity=70是怎么算的?请多多指教,谢谢!
import java.io.*;
public class capacity
{
public static void main(String[] args)
{
StringBuffer buffer = new StringBuffer("Hello,how are you?");
String output= "buffer="+buffer.toString()+"\nlength="+buffer.length()+"\ncapacity="+buffer.capacity();
buffer.ensureCapacity(69);
output+= "\n\nNew capacity="+buffer.capacity();
buffer.setLength(10);
output+= "\n\nNew length="+buffer.length()+"\nbuffer="+buffer;
System.out.println(output);
}}
程序运行结果如下:
buffer=Hello,how are you?
length=18
capacity=34New capacity=70New length=10
buffer=Hello,how
其中length=18,New length=10明白,capacity=34和New capacity=70是怎么算的?请多多指教,谢谢!
public void ensureCapacity(int minimumCapacity)Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns. Parameters:
minimumCapacity - the minimum desired capacity.
--------------larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2. ///////
String output= "buffer="+buffer.toString()+"\nlength="+buffer.length()+"\ncapacity="+buffer.capacity();
buffer.ensureCapacity(69);
output+= "\n\nNew capacity="+buffer.capacity();
第一次capacity是34,通过ensureCapcity之后,34<69,所以newCapcity=34*2+2=70
结贴给分吧,谢谢。
super(str.length() + 16);
append(str);
}--------
这个是StringBuffer以String为参数的constructor的源码,34=18+16,所以其实capcity是34
之后ensureCapcity之后就是34*2+2=70
初始是容量是 16(java.lang.StringBuffer 的默认构造),放不下了,就加 1 再乘以 2 来扩大容量(java.lang.AbstractStringBuilder 的 expandCapacity 方法,StringBuffer 的父类)。
-----------
这个理解有点问题,这里34刚好是18+16=(16+1)*2,是凑巧,
初始构造的时候是根据参数String的长度+16来确定capcity的,不信去掉一个字符,结果会是17+16=33,而不是17》16==>capcity=(16+1)*2=34
如果字符串减少一个,33*2+2=69《69,newcapcity就会是69而不是68
System.out.println(sb.capacity()); // 输出 19sb.append("aabadfasfasfsadfadffdadfsasdasd");
System.out.println(sb.capacity()); // 输出 40sb.append("aabadfsssssssssddddddadfasdfadfasdfasdfasadfasfasd");
// 输出 84(因为添加后总的字符数超过了 (40 + 1)×2 = 82 了,就以实际字符数计算)
System.out.println(sb.capacity());sb.append("aabadfssssssssssdfsdasdfsasdasdfsdasdfsasdasdfsasdasd");
System.out.println(sb.capacity()); // 输出 170
原来的Tcapacity x 2 + 2.
believefym解释的比较好懂,我按他的解释进行了验证,认为他解释的比较正确,谢了!
不过也要谢谢bao110908和bit1010,谢谢!