使用 StringBuffer 的 getChars()

发布于 2024-12-20 18:02:44 字数 730 浏览 5 评论 0原文

我是 Java 新手。 我成功执行了以下程序,但我不明白输出。 这就是程序。

public class StringBufferCharAt {
    public static void main(String[] args)
    {
        StringBuffer sb = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
        System.out.println("Length of sb : " + sb.length());

        int start = 0;
        int end   = 10;

        char arr[] = new char[end - start];
        sb.getChars(start, end, arr, 0);
        System.out.println("After altering : "+ arr.toString());

    }
}

执行该程序后: 我得到以下输出:

Length of sb : 26
After altering : [C@21a722ef

我的问题:

  1. 在输出中不是打印 10 个字符,而是打印 11 个字符。
  2. 而不是打印原始字符“abcdefghij” 在sb里面,为什么我得到了一些其他的角色。

I am new to Java.
I executed the below program successfully but I don't understand the output.
This is the program.

public class StringBufferCharAt {
    public static void main(String[] args)
    {
        StringBuffer sb = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
        System.out.println("Length of sb : " + sb.length());

        int start = 0;
        int end   = 10;

        char arr[] = new char[end - start];
        sb.getChars(start, end, arr, 0);
        System.out.println("After altering : "+ arr.toString());

    }
}

After executing this program: I got the following output:

Length of sb : 26
After altering : [C@21a722ef

My Questions:

  1. Instead of printing 10 characters in the output, why 11 characters.
  2. Instead of printing the original characters "abcdefghij" which are
    inside sb, why did I get some other characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

肤浅与狂妄 2024-12-27 18:02:44

最后一句中的 arr.toString() 为您提供了对象的字符串值(doc 这里),这是一个数组。您可能想要实现的目标类似于 Arrays.toString(arr) ,它将打印数组的内容(doc)。

The arr.toString() in your last sentence is giving you a String value of your Object (doc here), here's an array. What you were probably trying to achieve was something like Arrays.toString(arr) which will print the content of your array (doc).

一曲琵琶半遮面シ 2024-12-27 18:02:44

为什么输出是 11 个字符,这些字符的含义是什么?

碰巧的是,由 Object#toString() 指定的 char[] 的字符串表示形式是 11 个字符:[C >表示它是一个char[]@表示后面的8个十六进制数字是内存中的地址。正如 JavaDoc 所说,

此方法返回一个等于以下值的字符串:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Class#getName() 对于字符数组返回 "C[",并且 默认 hashCode() 实现(通常)返回对象在内存中的地址:

这通常是通过将对象的内部地址转换为整数来实现的,但 Java™ 编程语言不需要这种实现技术。


这应该如何解决呢?

如果要打印数组的内容,请使用 Arrays.toString()

// Instead of this:
System.out.println("After altering : "+ arr.toString());
// Use this:
System.out.println("After altering : "+ Arrays.toString(arr));

Why is the output 11 characters, and what do those characters mean?

It just so happens that the string representation of the char[], as specified by Object#toString(), is 11 characters: the [C indicates that it's a char[], the @ indicates the following 8 hex digits are an address in memory. As the JavaDoc states,

This method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Class#getName() returns "C[" for a char array, and the default hashCode() implementation (generally) returns the object's address in memory:

This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.


How should this be solved?

If you want to print the contents of an array, use Arrays.toString():

// Instead of this:
System.out.println("After altering : "+ arr.toString());
// Use this:
System.out.println("After altering : "+ Arrays.toString(arr));
哆兒滾 2024-12-27 18:02:44

您需要有这样的最终打印语句:

System.out.println("After altering : "+ new String(arr));

OR

System.out.println("After altering : "+ java.util.Arrays.toString(arr));

OUTPUT

For 1st case: abcdefghij
For 2nd case: [a, b, c, d, e, f, g, h, i, j]

注意: arr.toString() 不会打印数组的内容,这就是为什么您需要从 char 数组构造一个新的 String 对象,就像我上面的回答一样,或者调用 Arrays.toString(arr)`。

You need to have final print statement like this:

System.out.println("After altering : "+ new String(arr));

OR

System.out.println("After altering : "+ java.util.Arrays.toString(arr));

OUTPUT

For 1st case: abcdefghij
For 2nd case: [a, b, c, d, e, f, g, h, i, j]

Note: arr.toString() doesn't print the content of array that's why you need to construct a new `String object from char array like in my answer above or callArrays.toString(arr)`.

枫以 2024-12-27 18:02:44

java 中的数组没有任何内置的“人类可读”的 toString() 实现。您看到的只是从数组的内存位置派生的标准输出。

将 char[] 转换为可打印内容的最简单方法就是用它构建一个字符串。

System.out.println("After altering : " + String.valueOf(arr));

Arrays in java do not have any built in 'human readable' toString() implementations. What you see is just standard output derived from the memory location of the array.

The easiest way to turn a char[] into something printable is to just build a string out of it.

System.out.println("After altering : " + String.valueOf(arr));
临走之时 2024-12-27 18:02:44

表达式 arr.toString() 不会使用 char[] 的内容将 char[] 转换为字符串;它使用默认的 Object.toString() 方法,该方法用于打印对象的表示形式(在本例中为数组对象)。您想要的是使用 new String(arr) 将字符转换为字符串,或者使用 Arrays.toString(arr) 将字符的数组表示形式转换为字符串。

The expression arr.toString() does not convert a char[] to a String using the contents of the char[]; it uses the default Object.toString() method, which is to print a representation of the object (in this case an array object). What you want is either to convert the characters to a String using new String(arr) or else an array representation of the characters using Arrays.toString(arr).

[旋木] 2024-12-27 18:02:44

我可以回答这个问题的一部分是#1。您将返回 11 个字符,因为开始和结束变量是索引。 0 是有效索引,因此从 0 - 10 有 11 个不同的数字。

The one part of this question I can answer is #1. You are getting back 11 characters because the start and end variables are indexes. 0 is a valid index so, from 0 - 10 there are 11 different numbers.

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