[填空题] 请完善程序(程序文件名:Java_1.java)并进行调试。请在下画线处填入正确内容,然后删除下画线。请勿删除注释行和其他已有的语句内容。 题目要求: 请完善程序并进行调试,使程序输出

11 查阅

[填空题] 请完善程序(程序文件名:Java_1.java)并进行调试。请在下画线处填入正确内容,然后删除下画线。请勿删除注释行和其他已有的语句内容。

题目要求:

请完善程序并进行调试,使程序输出结果如下:

源程序:

import javax.swing.*;

public class Java_1

public static void main(String args[])

StringBuffer buf=new (1) ("Hello,how are you");

String output="bur="+bur.toString()+"length="+bur.length()+"incapacity="+bur.capacity();

bur.ensureCapacity(75);

output+="New capacity="+buf.capacity();

buf.setLength(10);

output+="ininNew length="+buf.length()+"inbuf="+bur.toString();

JOptionPane.showMessageDialog(null, output, "字符串缓存长度和容量的实例", JOptionPane. (2) );

System. (3) (0);

参考答案:

stringBuffer

本程序主要考查stringBuffer类的几个常用方法,然后用对话框输出结果。 public string toString()——返回此序列中数据的字符串表示形式。 public int length()——返回长度(字符数)。 public int capacity()——返回当前容量。 public void ensureCapacity(int minimumCapacity)——确保容量至少等于指定的最小值。 public void setLength(int newLength)——设置字符序列的长度。 由程序可看出buf是stringBuffer类的对象,所以需要用构造方法进行实例化,其构造方法:public stringBuffer(string str)构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。

计算机考试