Java StringBuffer 问题
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中构造和创建的意思是一样的吗?
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[]
This is only part of the code, I didn't want to post the whole thing. I just to focus on this and then then move on when I fully understand it.
1) 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");
?
2) Does construct and create mean the same thing in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些东西中的任何一个都会创建一个字符串吗? (提示:否)
如何在 Java 中创建特定类的实例? (提示:查看有关
new
的注释。提示 2:查看java.lang.String
类。)。Java 对象是由构造函数创建的,因此可以合理地互换使用“构造”和“创建”。
(从技术上讲,它们的含义并不完全相同。对象是通过分配内存并调用类构造函数来创建的。因此,如果您很迂腐,那么构造是创建对象过程的一部分但没必要迂腐。)
Do either of those things create a String? (Hint: no)
How do you create an instance of a particular class in Java? (Hint: review your notes on
new
. Hint 2: look at the javadocs for thejava.lang.String
class.).Java objects are created by a constructor, so it is reasonable to use "construct" and "create" interchangeably.
(Technically they don't mean exactly the same thing. An object is created by allocating its memory and calling a class constructor. So if you are being pedantic, constructing is part of the process of creating an object. But there's no need to be pedantic.)
1) 从
chars
创建字符串的简单方法:注意,您可能不能只使用
String(char[])
构造函数,因为String(char[])
构造函数的存在code>length 成员意味着不能使用chars
的某些元素。如果您不指定要使用的元素,则可能会在字符串末尾出现额外的字符(可能是 NUL,也可能是垃圾)。2)不完全是。虽然对象是在调用构造函数时创建的(为对象本身分配的内存),但那时它只是一块内存。在构造函数返回之前,对象尚未完全构造(阅读:任何类不变量可能不一定成立)。这意味着两者之间的区别仅适用于构造函数,因此人们通常几乎可以互换使用“创建”和“构造”。
1) The easy way to create a string from
chars
:Note, you probably can't just use the
String(char[])
constructor, because the existence of thelength
member implies that some elements ofchars
may not be used. If you don't specify what elements to use, you could end up with extra chars (maybe NULs, maybe garbage) at the end of the string.2) Not exactly. While the object is created (the memory for the object itself allocated) at the time the constructor's called, at that time it's just a blob of memory. The object is not fully constructed (read: any class invariants may not necessarily hold) until the constructor returns. This means the difference between the two only applies in the constructor, though, so it's common for people to use "create" and "construct" almost interchangeably.
您需要一个字符数组,因为您必须将字符存储在某个地方。您可以将它们存储在
String
中,但是您几乎不需要StringBuffer
,其存在的理由是比aString
(通常使用更高效的StringBuffer
来构造字符串,然后使用.toString()
获取真正的String
对象)。该长度将用于指定 StringBuffer 的长度。您可能认为您可以只使用字符数组的长度,但在减少 StringBuffer 的大小时,它实际上更有效,而不是重新分配较小的数组,特别是如果您最终可能会这样无论如何,还需要更多。
此方案所需的
String
构造函数为String (char[] value, int offset, int count)
使用偏移量零和长度
的计数。这将允许您仅使用char
数组的所需子集。You need an array of chars because, well, you have to store the characters somewhere. You could store them in a
String
but then you would have little need of aStringBuffer
, whose raison d'etre is to be more efficient than aString
(you generaly use the more efficientStringBuffer
to construct a string, then use.toString()
to get a realString
object).The length would be used to specify the length of the
StringBuffer
. You may thing you could just use the length of the character array but it's actually more efficient, when reducing the size of theStringBuffer
, to not re-allocate a smaller array, especially if you may just end up needing more anyway.The
String
constructor you would need for this scheme would beString (char[] value, int offset, int count)
using an offset of zero and a count oflength
. This would allow you to use only the desired subset of thechar
array.正如他所说,查看 API,您会发现一个采用字符数组的 String 构造函数:http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String .html#String%28char[]%29 。
As he says, have a look at the API, where you will find a String constructor that takes an array of characters: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#String%28char[]%29 .