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