java 字符串方法 Append() : StringBuilder vs StringBuffer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17173314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String method Append() : StringBuilder vs StringBuffer
提问by zee
With this code:
使用此代码:
public static void main(String[] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s);
change(buffer);
System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(3));
System.out.println(buffer);
}
private static void change(StringBuilder buffer) {
buffer.append(" and HTML");
}
When I run the code using StringBuilder I get error message
当我使用 StringBuilder 运行代码时,我收到错误消息
The constructor StringBuilder(String) is undefined
The method charAt(int) is undefined for the type StringBuilder
Tried StringBuffer instead and it works. The content of the StringBuffer object is compiled to "Java and Eclipse.."
改为尝试 StringBuffer 并且它有效。StringBuffer 对象的内容被编译为“Java 和 Eclipse..”
public static void main(String[] args) {
String s = "Java";
StringBuffer strbuf = new StringBuffer(s);
change(strbuf);
System.out.println("The Stringbuffer.charAt(5) is ? " + strbuf.charAt(3));
System.out.println(strbuf);
}
private static void change(StringBuffer strbuf) {
strbuf.append(" and Eclipse");
}
}
}
回答by fge
StringBuilder
doeshave a constructor accepting a String
as an argument, and doeshave a .charAt()
method(which it it must implement since it implements CharSequence
).
StringBuilder
确实有一个接受 aString
作为参数的构造函数,并且确实有一个.charAt()
方法(它必须实现,因为它实现了CharSequence
)。
Conclusion: this is a mishap from the part of your IDE, which did not import the correct StringBuilder
. You use another library which has the unfortunate "property" of having implemented a class by the same name -- but not in the same package.
结论:这是 IDE 部分的失误,它没有导入正确的StringBuilder
. 您使用另一个库,它具有实现同名类的不幸“属性”——但不在同一个包中。
Go see at the top of your file if the import line is:
如果导入行是:
import java.lang.StringBuilder;
回答by Amin Abu-Taleb
You might have imported a wrong StringBuilder class instead of java.lang.StringBuilder which does have a StringBuilder(String) constructor and charAt(int) method.
您可能导入了错误的 StringBuilder 类,而不是 java.lang.StringBuilder ,后者确实具有 StringBuilder(String) 构造函数和 charAt(int) 方法。
Could you check your import. You should have this one
你能检查一下你的进口吗?你应该有这个
import java.lang.StringBuilder;
回答by Some Java Guy
Make sure that you are notdefining your class name as StringBuilder
确保您没有将类名定义为StringBuilder
For Example: Even if you import it correctly
例如:即使正确导入
import java.lang.StringBuilder;
But if you write your class as
但是如果你把你的课写成
public class StringBuilder { //If class name matches below Object Creation
public static void main(String[] args) {
String s = "Java";
StringBuilder buffer = new StringBuilder(s); //Object creation
change(buffer);
System.out.println("What's strBuf.charAt(5)? " + strBuf.charAt(3));
System.out.println(buffer);
}
private static void change(StringBuilder buffer) {
buffer.append(" and HTML"); //you will get this error at append
//The method append(String) is undefined for the type StringBuilder
}
}
Suggestions
建议
Rename your class name to something else but notStringBuilder
将您的班级名称重命名为其他名称,但不要StringBuilder