neumeng 2010-03-05 15:06
浏览 283
已采纳

stringbuffer 中的capacity()函数

public class HelloWord{
public static void main(String[] args){

StringBuffer sb = new StringBuffer("abcdefgh");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
sb.setLength(30);
System.out.println("set the length is 30");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
sb.setLength(60);
System.out.println("set the length is 60");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
sb.setLength(120);
System.out.println("set the length is 120");
System.out.println("the length is "+sb.length());
System.out.println("the capacity is "+sb.capacity());
}
}

结果是
the length is 8
the capacity is 24
set the length is 30
the length is 30
the capacity is 50
set the length is 60
the length is 60
the capacity is 102
set the length is 120
the length is 120
the capacity is 206
这是为什么呢

  • 写回答

1条回答 默认 最新

  • iteye_13500 2010-03-05 15:59
    关注

    研究StringBuffer的源码就清楚了:

    [code="java"] char value[];
    public synchronized int capacity() {
    return value.length;
    }[/code]
    所以capacity返回的字符数组value的长度,value用来保存StringBuffer实际的字符,这和String的实现是一样的。

    构造:
    [code="java"] public StringBuffer(String str) {
    super(str.length() + 16); // 这里设置了value的长度, 8 + 16 正好是24
    append(str);
    }

    // AbstractStringBuilder的构造
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }[/code]
    

    setLength:
    [code="java"] public synchronized void setLength(int newLength) {
    super.setLength(newLength);
    }

    // AbstractStringBuilder的setLength方法
    public void setLength(int newLength) {
    if (newLength < 0)
        throw new StringIndexOutOfBoundsException(newLength);
    if (newLength > value.length)
        expandCapacity(newLength);
    
    if (count < newLength) {
        for (; count < newLength; count++)
        value[count] = '\0';
    } else {
            count = newLength;
        }
    }
    
    // AbstractStringBuilder的expandCapacity方法
    void expandCapacity(int minimumCapacity) {
    int newCapacity = (value.length + 1) * 2;
        if (newCapacity < 0) {
            newCapacity = Integer.MAX_VALUE;
        } else if (minimumCapacity > newCapacity) {
        newCapacity = minimumCapacity;
    }
        value = Arrays.copyOf(value, newCapacity);
    }[/code]
    

    看第一句就行了:
    初始的capability是24,当你用
    sb.setLength(30);
    后,capability = (24+1)*2=50
    接着:
    capability = (50+1)*2=102
    capability = (102+1)*2=206
    ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。