问题描述
我正在尝试打印七个字符串的数组,并使用get方法将它们返回到打印前的分钟,但是每当我运行它时,控制台上都会出现一些乱码:[Ljava.lang.String; @ 6d6de4e1
I'm trying to print an array of seven strings, and am using a get method to return them to the min before printing, but whenever I run it some random gibberish shows up on the console: [Ljava.lang.String;@6d6de4e1
这是我的get方法
String[] getStuff(){
return stuff;
}
这是主类的打印方法:
System.out.println(trex.getStuff());
该数组是完全有效的,并且充满了字符串,因此我不确定该错误是什么.
The array is completely valid and full of Strings, so I'm not sure what this error is.
推荐答案
您要打印内部对象的字符串值,而不是数组的字符串值.幸运的是,Java为此提供了一个内置函数:Arrays.deepToString()
You want to print the string values of the interior objects, not the string value of the array. Luckily java has a builtin for this: Arrays.deepToString()
因此您的打印代码应为:
So your print code should be:
System.out.println(Arrays.deepToString(trex.getStuff()));
这篇关于Java:如何打印整个字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!