Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生成concat运算符以将char数组添加到字符串。打印字符串对象的引用变量_Java_Arrays - Fatal编程技术网

Java 生成concat运算符以将char数组添加到字符串。打印字符串对象的引用变量

Java 生成concat运算符以将char数组添加到字符串。打印字符串对象的引用变量,java,arrays,Java,Arrays,到目前为止,如果我尝试使用concat操作符,我会得到这个错误 java:14:找不到符号 符号:方法concat(MyString) 位置:类MyString System.out.println(hello.concat(再见));//打印“hellogoodbye” 当我试图打印MyString的“hello”对象时,我得到MyString@558ee9d6 我觉得很快就要开始工作了 public class MyString { private char[] charStri

到目前为止,如果我尝试使用concat操作符,我会得到这个错误 java:14:找不到符号 符号:方法concat(MyString) 位置:类MyString System.out.println(hello.concat(再见));//打印“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;
    }


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

如果希望/需要
oneString
属性包含
String
表示的
charString
,则可以使用char数组创建
String

this.oneString = new String(charString);
还有这条线

System.out.println(hello);
将打印对象的
字符串
表示。您需要重写
MyString
类中
Object
类中的
public String toString()
方法:

@Override
public String toString() {
    return this.oneString; //or similar
}

您正在尝试打印对象:

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 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
    }
    }

我想说,现在,您正在尝试向字符串添加字符数组。调用s.toString()不会使char[]成为字符串。那不会像你现在的样子。试一试

result = oneString + new String(s);

你知道为什么我在尝试运行concat时会出现“找不到符号”错误吗?@FredV这是因为你的
concat
方法接收到一个
char[]
String
参数,而不是
MyString
,你已经有了MyString再见了。你可能应该在发布代码之前校对一下代码。简单的编译可以帮助您解决很多问题。是否需要内部
charString
?这只会使你的内部状态复杂化,只保留一个
字符串
会让事情简单得多。
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
    }
    }
result = oneString + new String(s);