0
 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だと思ったのですが、うまくいきませんでした。

ありがとう :)

4

1 に答える 1

0

この宿題の目的は、を使用することではなく、StringBufferに基づいて独自の宿題を作成することchar[]です。

たとえば、を使用しappend(char c)て、配列の最後に指定された文字を追加しchars、長さをインクリメントするコードを作成することになっています(文字を追加したばかりであるため)。

この割り当ては、StringBufferの独自の実装を作成し、それを直接使用するのではなく、どのように機能するかを理解することです。

于 2010-10-16T11:23:01.573 に答える