gpt4 book ai didi

java - 使用容量作为参数的 stringBuffer 插入方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:44 25 4
gpt4 key购买 nike

public class example{
public static void main(String args[]) {
StringBuffer s1 = new StringBuffer(10);
s1.insert(0,avaffffffffffffffffffffffffffffffffffffffffffvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
System.out.println(s1);
}
}

the output of this code is coming as avaffffffffffffffffffffffffffffffffffffffffffvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv.

StringBuffer 类的方法中的参数 10 有什么用?如果 10 是 Buffer 的大小,0 是插入方法的偏移量,那么我们如何将整个字符串作为输出?

最佳答案

来自 JavaDoc:

A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls

10 只是初始容量(继续阅读 JavaDoc):

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

关于java - 使用容量作为参数的 stringBuffer 插入方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26863771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com