Java StringBuffer 问题

发布于 2024-09-27 21:24:48 字数 685 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不离久伴 2024-10-04 21:24:48

当他说从“chars”数据成员构造一个新字符串时,他是什么意思?我很困惑他要我做什么。我应该对 char charAt(int index) 做些什么吗?或类似的东西: StringBuffer sb = new StringBuffer("test");?

这些东西中的任何一个都会创建一个字符串吗? (提示:否)

如何在 Java 中创建特定类的实例? (提示:查看有关 new 的注释。提示 2:查看 java.lang.String 类。)。

Java 中构造和创建的意思是一样的吗?

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");?

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 the java.lang.String class.).

Does construct and create mean the same thing in Java?

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.)

画▽骨i 2024-10-04 21:24:48

1) 从 chars 创建字符串的简单方法:

String result = new String(chars, 0, length);

注意,您可能不能只使用 String(char[]) 构造函数,因为 String(char[]) 构造函数的存在code>length 成员意味着不能使用 chars 的某些元素。如果您不指定要使用的元素,则可能会在字符串末尾出现额外的字符(可能是 NUL,也可能是垃圾)。

2)不完全是。虽然对象是在调用构造函数时创建的(为对象本身分配的内存),但那时它只是一块内存。在构造函数返回之前,对象尚未完全构造(阅读:任何类不变量可能不一定成立)。这意味着两者之间的区别仅适用于构造函数,因此人们通常几乎可以互换使用“创建”和“构造”。

1) The easy way to create a string from chars:

String result = new String(chars, 0, length);

Note, you probably can't just use the String(char[]) constructor, because the existence of the length member implies that some elements of chars 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.

夏见 2024-10-04 21:24:48

您需要一个字符数组,因为您必须将字符存储在某个地方。您可以将它们存储在String中,但是您几乎不需要StringBuffer,其存在的理由是比a String (通常使用更高效的 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 a StringBuffer, whose raison d'etre is to be more efficient than a String (you generaly use the more efficient StringBuffer to construct a string, then use .toString() to get a real String 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 the StringBuffer, 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 be String (char[] value, int offset, int count) using an offset of zero and a count of length. This would allow you to use only the desired subset of the char array.

风铃鹿 2024-10-04 21:24:48

正如他所说,查看 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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文