class MyStringBuffer {
//TODO explain: why you would need these data members.
private char[] chars; //character storage.
private int length; //number of characters used
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[]
这只是代码的一部分,我不想发布整个代码。我只是专注于这一点,然后当我完全理解它时继续前进。
1) 当他说从 'chars' 数据成员构造一个新的 String
时,他的意思是什么?我很困惑他要我做什么。我应该用 char charAt(int index) 做点什么吗?或类似的东西:StringBuffer sb = new StringBuffer("test");
?
2)构造和创建在Java中的意思是一样的吗?
最佳答案
What does he mean when he said construct a new String from the 'chars' data member? I'm confused as to what he wants me to do. Am I suppose to do something with char charAt(int index)? or something like this: StringBuffer sb = new StringBuffer("test");?
这些东西中的任何一个都会创建一个字符串吗? (提示:否)
如何在 Java 中创建特定类的实例? (提示:查看有关 new
的注释。提示 2:查看 java.lang.String
类的 javadoc。)。
Does construct and create mean the same thing in Java?
Java 对象是由构造函数创建的,因此可以合理地互换使用“构造”和“创建”。
(从技术上讲,它们的含义并不完全相同。对象是通过分配内存并调用类构造函数来创建的。因此,如果您很迂腐,那么构造是过程的一部分创建一个对象。但没有必要迂腐。)
关于Java StringBuffer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3948292/