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

标签 java

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/

相关文章:

java - 如何在 Intellij Idea 中调用删除方法?

java - 如何在使用 rs.getString 时修复 'terminated'

java - Hibernate - 一个设计问题

java - 带有 bouncycaSTLe 库的 jar 文件 : NoClassDefFoundError

java - netty-tcnative 没有在 jdk.tls.disabledAlgorithms 中选择配置

java - 无法解析符号 'Before' - Intellij jUnit

java - Spring Boot @MockBeans - 如何在多个测试类中使用相同的 beans

java - 从配置加载凭据

java - 将 OnMapClickListener 与 Intent Activity 结合使用

java - org.springframework.web.servlet.PageNotFound noHandlerFound 未找到带有 URI 的 HTTP 请求的映射。我已将项目部署为 Maven 项目