【问题标题】:Java StringBuffer questions: How do I append something in this situation?Java StringBuffer 问题:在这种情况下如何追加内容?
【发布时间】:2010-10-16 11:19:25
【问题描述】:
 package homework5;


import java.io.*;
import java.util.Arrays;
public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    MyStringBuffer strTest = new MyStringBuffer();
    // FIX ME if you see the following string is not on the same line
    System.out.println("stringTest is initilized - capacity=" + strTest.capacity() + " length=" + strTest.length());
    BufferedReader stdin = new BufferedReader(
                         new InputStreamReader( System.in ) );
    System.out.print("Enter a string:");
    String myString = stdin.readLine();
    strTest.append(myString); //TOTEST: test your append (String str)
    printStrTest(strTest);
    while (true) {
        // FIX ME if you see the following string is not on the same line
        System.out.println("Enter 1 of 4 options: ac (append a char), as (append a string), i (insert), d (delete), r (reverse), q (quit)");
        String opt = stdin.readLine();
        if (opt.equals("ac")) {
            System.out.print("Append a char:");
            char c = stdin.readLine().charAt(0);
            strTest.append(c); //TOTEST: test your append (char a) function
            printStrTest(strTest);
        } else if (opt.equals("as")) {
            System.out.print("Append a string:");
            String aStr = stdin.readLine();
            strTest.append(aStr); //TOTEST: test append with expandation
            printStrTest(strTest);
        } else if (opt.equals("i")) {
            System.out.print("Insert a char:");
            char c = stdin.readLine().charAt(0);
            System.out.print("Location:");
            int loc = Integer.parseInt(stdin.readLine());
            strTest.insert(loc, c); //TOTEST: test your insert
            printStrTest(strTest);
        } else if (opt.equals("d")) {
            System.out.print("Delete at location:"); // TOTEST delete
            int loc = Integer.parseInt(stdin.readLine());
            strTest.deleteCharAt(loc);
            printStrTest(strTest);
        } else if (opt.equals("r")) {
            strTest.reverse(); //TOTEST reverse
            printStrTest(strTest);
        } else if (opt.equals("q")) {
            System.out.println("Goodbye!!!");
            break;
        } else {
            System.out.println("Error option entered:" + opt);
        }
    }
}

static void printStrTest(MyStringBuffer strTest){
    // FIX ME if you see the following string is not on the same line
    System.out.println("New string:" + strTest.toString()+ ",cap=" + strTest.capacity() + " len=" + strTest.length());
}
}

class MyStringBuffer {
//TODO explain: why you would need these data members.
private char[] chars; //character storage. 
private int length;   //number of characters used.  efficient

public MyStringBuffer(){
    chars = new char[16]; //Default storage is 16
    length  = 0; // No char
}

public int capacity(){
    return chars.length;
}

//Expanse the capcity of the chars storage
void expandCapacity(int minimumCapacity) {
int newCapacity = (chars.length + 1) * 2;
    if (newCapacity < 0) {
        newCapacity = Integer.MAX_VALUE;
    } else if (minimumCapacity > newCapacity) {
    newCapacity = minimumCapacity;
}
    chars = Arrays.copyOf(chars, newCapacity);
}

public int length(){
    return length;
}

public String toString(){
    //TODO

    //Hint: just construct a new String from the ‘chars’ data member
    //and return this new String – See API online for how create
    //new String from char[]

String result = new String(chars, 0, length);
return result;
}

public MyStringBuffer append (char c){

    //TODO
    //You will need to call the expandCapacity if necessary

    return this;
}

public MyStringBuffer append (String str){
    //TODO
    //You will need to call the expandCapacity if necessary

    return this;
}

public MyStringBuffer insert (int offset, char c){
    //TODO
    //You will need to call the expandCapacity if necessary

    return this;
}

public MyStringBuffer deleteCharAt(int index) {
    //TODO

    return this;
}

public MyStringBuffer reverse (){
    //TODO

    return this;
}
}

嗨,我读过这个http://www.roseindia.net/java/beginners/StringBuffer.shtml,我知道如何在这些情况下追加内容,但我不知道如何在这个情况下应用它。我想参考什么?我想我应该做这样的事情: strbuf.append("Hello");但是我放什么代替strbuf?我尝试了一些差异参考,但他们一直说找不到变量。有人可以给我看吗?我很确定我可以完成剩下的工作。或者至少我希望我可以。我以为是 myString ,但没有用。

谢谢:)

【问题讨论】:

    标签: java stringbuffer


    【解决方案1】:

    在本作业中,目标不是使用StringBuffer,而是基于char[] 创建自己的。

    例如使用append(char c),您应该编写一个代码,将给定的字符添加到数组chars 的末尾并增加长度(因为您刚刚添加了一个字符)。

    这个作业是关于创建你自己的 StringBuffer 实现并了解它是如何工作的,而不是直接使用它。

    【讨论】:

    • @Colin Hebert 很抱歉,我误会了吗?您将实现 MyStringBuffer 类,它是 StringBuffer 类的简化版本(参见讲义:cis36L052)。框架已经提供。您将需要实现 6 个函数:append (char c)、append (String str)、insert (int offset, char c)、deleteCharAt(int index)、reverse() 和 toString()。您必须调试、修复如果您无法构建程序,则会出现错误。
    • 框架为你提供。您需要将以下代码复制到您的新程序中。每当您看到 //FIX ME 或 //TODO cmets 时,您都需要修复代码或添加新代码。如果无法构建程序,则必须调试,修复错误。
    • @CuriousStudent,它说你将实现StringBuffer的简化版本,而不是使用StringBuffer
    • 哦,哇,该死的,基本上我所要做的就是:roseindia.net/java/beginners/StringBuffer.shtml ?
    • @CuriousStudent,好吧,这又是一个使用StringBuffer的类。您不必(实际上您不必)使用 StringBuffer 类,而是创建自己的类
    猜你喜欢
    • 2012-10-11
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多