使用 StringBuffer 的 getChars()
我是 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
我的问题:
- 在输出中不是打印 10 个字符,而是打印 11 个字符。
- 而不是打印原始字符“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:
- Instead of printing 10 characters in the output, why 11 characters.
- Instead of printing the original characters "abcdefghij" which are
inside sb, why did I get some other characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最后一句中的
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 likeArrays.toString(arr)
which will print the content of your array (doc).为什么输出是 11 个字符,这些字符的含义是什么?
碰巧的是,由
Object#toString()
指定的char[]
的字符串表示形式是 11 个字符:[C
>表示它是一个char[]
,@
表示后面的8个十六进制数字是内存中的地址。正如 JavaDoc 所说,Class#getName()
对于字符数组返回"C["
,并且 默认hashCode()
实现(通常)返回对象在内存中的地址:这应该如何解决呢?
如果要打印数组的内容,请使用
Arrays.toString()
: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 byObject#toString()
, is 11 characters: the[C
indicates that it's achar[]
, the@
indicates the following 8 hex digits are an address in memory. As the JavaDoc states,Class#getName()
returns"C["
for a char array, and the defaulthashCode()
implementation (generally) returns the object's address in memory:How should this be solved?
If you want to print the contents of an array, use
Arrays.toString()
:您需要有这样的最终打印语句:
OR
OUTPUT
注意:
arr.toString()
不会打印数组的内容,这就是为什么您需要从 char 数组构造一个新的 String 对象,就像我上面的回答一样,或者调用 Arrays.toString(arr)`。You need to have final print statement like this:
OR
OUTPUT
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 call
Arrays.toString(arr)`.java 中的数组没有任何内置的“人类可读”的 toString() 实现。您看到的只是从数组的内存位置派生的标准输出。
将 char[] 转换为可打印内容的最简单方法就是用它构建一个字符串。
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.
表达式 arr.toString() 不会使用 char[] 的内容将 char[] 转换为字符串;它使用默认的
Object.toString()
方法,该方法用于打印对象的表示形式(在本例中为数组对象)。您想要的是使用 new String(arr) 将字符转换为字符串,或者使用 Arrays.toString(arr) 将字符的数组表示形式转换为字符串。The expression
arr.toString()
does not convert achar[]
to a String using the contents of thechar[]
; it uses the defaultObject.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 usingnew String(arr)
or else an array representation of the characters usingArrays.toString(arr)
.我可以回答这个问题的一部分是#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.