1

到目前为止,如果我尝试使用 concat 运算符,我会收到此错误 Tester.java:14: cannot find symbol symbol : method concat(MyString) location: class MyString System.out.println(hello.concat(goodbye)); // 打印“hellogoodbye”

当我尝试打印 MyString 的“hello”对象时,我得到 MyString@558ee9d6 我觉得它离工作太近了。

public class MyString

{
     private char[] charString;
     private String oneString;


      public  MyString(String string) 
      {
        this.oneString = string;


      }

//second constructor for overloading
    public MyString(char[] s) 
    {

        this.charString = s;
        this.oneString = charString.toString();
    }

//methods
    public String toString( char [] s)
    {
        return new String(s);
    }

    public char charAt(int i) {

        char [] temp = new char[oneString.length()];
        for ( int j = 0; j < oneString.length() ;  j++)
        temp[j] = oneString.charAt(i);
        return temp[i];
    }

    public String concat ( char[] s)
    {
        s.toString();
        String result = oneString + s;
        return result;
    }

    public String concat ( String s)
    {
        String result = oneString + s;
        return result;
    }


}

public class Tester { public static void main (String[] args)

    {
    MyString hello = new MyString("hello");
    System.out.println(hello);  // right now this prints MyString@558ee9d6
    System.out.println(hello.charAt(0));    // works, prints 'h'
    char[] arr = {'g','o','o','d','b','y','e' };
    MyString goodbye = new MyString(arr);
   // System.out.println(hello.concat(goodbye)); // i can't get this line to work
    System.out.println(hello.equals(goodbye)); // works, prints false
    System.out.println(hello.equals(hello));  //works, prints true
    }
    }
4

3 回答 3

1

charString.toString()将为您提供对象的String表示形式char[],它不会将您char[]转换为String.

如果您想要/需要该oneString属性包含您的String表示形式charString,那么您可以String使用您的 char 数组创建一个:

this.oneString = new String(charString);

另外,这条线

System.out.println(hello);

将打印String您的对象的表示。您需要在类中覆盖类中的public String toString()方法:ObjectMyString

@Override
public String toString() {
    return this.oneString; //or similar
}
于 2012-11-14T01:43:45.817 回答
1

您正在尝试打印 Object :

System.out.println(hello);  // right now this prints MyString@558ee9d6

在这种情况下,您的 MyString 类

将 get 方法设为变量 oneString。

public String getOneString() {return this.oneString;}

然后打电话

System.out.println(hello.getOneString());

另一个问题

System.out.println(hello.concat(goodbye));

你 concat 方法接收一个字符串而不是一个 MyString 类

你可能想这样做

System.out.println(hello.concat(goodbye.getOneString()));

或者

public String concat ( MyString myS)
    {
        String s = myS.getOneString();
        String result = oneString + s;
        return result;
    }

最后结果:

public class Tester { public static void main (String[] args)

    {
    MyString hello = new MyString("hello");
    System.out.println(hello.getOneString());
    System.out.println(hello.getOneString().charAt(0)); 
    char[] arr = {'g','o','o','d','b','y','e' };
    MyString goodbye = new MyString(arr);
    System.out.println(hello.concat(goodbye.getOneString()));
    System.out.println(hello.equals(goodbye)); // works, prints false
    System.out.println(hello.equals(hello));  //works, prints true
    }
    }
于 2012-11-14T01:43:47.910 回答
0

我会说现在,您正在尝试将字符数组添加到字符串中。调用 s.toString() 不会使 char[] 成为字符串。这不会像你拥有的那样工作。尝试

result = oneString + new String(s);
于 2012-11-14T01:48:37.210 回答