⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringbuffercaplen.java

📁 金旭亮的java教案
💻 JAVA
字号:
// StringBufferCapLen.java
// This program demonstrates the length and
// capacity methods of the StringBuffer class.
import javax.swing.*;

public class StringBufferCapLen {
   public static void main( String args[] )
   {
      StringBuffer buf =
         new StringBuffer( "Hello, how are you?" );

      //length:当前字符个数
      //capacity:不另外分配内存可以存放的字符个数
      
      String output = "buf = " + buf.toString() +
                      "\nlength = " + buf.length() +
                      "\ncapacity = " + buf.capacity();
                      
	  //ensureCapacity:保证StringBuffer的最小容量
      buf.ensureCapacity( 75 );
      output += "\n\nNew capacity = " + buf.capacity();
	  
	  //setLength:增大或减小StringBuffer的长度
      buf.setLength( 10 );
      output += "\n\nNew length = " + buf.length() +
                "\nbuf = " + buf.toString();

      JOptionPane.showMessageDialog( null, output,
         "StringBuffer length and capacity Methods",
         JOptionPane.INFORMATION_MESSAGE );

      System.exit( 0 );
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -