java 为什么我的数组不能正确打印?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16217452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't my array print out correctly?
提问by user2320169
I am trying to write a simple program using the code below to make a single dimensional array that you can then call a value from using the index numbers. I am using javaand eclipseas my compiler. Whenever I try to debug or run the program, the full array prints out as this: [I@1fa8d3b
.
我正在尝试使用下面的代码编写一个简单的程序来制作一个一维数组,然后您可以使用索引号调用一个值。我使用java和eclipse作为我的编译器。每当我试着调试或运行程序,全阵列打印出来,因为这:[I@1fa8d3b
。
class Array
{
public static void main(String[] args)
{
int[] MyArray = new int[] {15, 45, 34, 78, 65, 47, 90, 32, 54, 10};
System.out.println("The full array is:");
System.out.println(MyArray);
System.out.println("The 4th entry in the data is: " + MyArray[3]);
}
}
The correct data entry prints out when it is called though. I have tried to look for answers online as to what I should do, but I could not find anything that actually works. I am just starting to learn Java so there could be a very simple answer to this that I am just overlooking. If anyone has any ideas, I would be greatly appreciative.
但是,在调用时会打印出正确的数据条目。我试图在网上寻找关于我应该做什么的答案,但我找不到任何真正有效的东西。我刚刚开始学习 Java,所以可能有一个非常简单的答案,我只是忽略了这一点。如果有人有任何想法,我将不胜感激。
回答by Kyle
Java is an object oriented language. When you are calling System.out.print(MyArray);
in Java you are actually printing the address of the object on the heap in memorythe toString code from it's parent class Object
, the code is shown below contributed by the comment from EngFouad, sorry for misspeaking. The weird String you see printed out is the reference the computer uses to find your data when you ask for something associated with the variable MyArray
.
Java 是一种面向对象的语言。当您System.out.print(MyArray);
在 Java中调用时,您实际上是在打印内存中堆上对象的地址来自它的父类的 toString 代码Object
,代码如下所示,由 EngFouad 的评论贡献,抱歉我说错了。您看到打印出来的奇怪字符串是当您要求与变量相关联的内容时计算机用来查找数据的引用MyArray
。
As stated by the other answers, to print out the data of your object you can use the Array
class's built in .toString()
method. This will print the data from the object instead of just the object reference.
正如其他答案所述,要打印出对象的数据,您可以使用Array
该类的内置.toString()
方法。这将打印来自对象的数据,而不仅仅是对象引用。
System.out.println(Arrays.toString(MyArray);
CorrectionActually it is toString() of the class Object: getClass().getName() + "@" + Integer.toHexString(hashCode()). – Eng.Fouad
更正实际上是类 Object 的 toString():getClass().getName() + "@" + Integer.toHexString(hashCode())。– Eng.Fouad
Mistake above was corrected thanks for the comments, hate to give the wrong information. I misunderstood it myself. Here is the API reference to see the code:
以上错误已更正,感谢您的评论,讨厌提供错误的信息。我自己理解错了。这是查看代码的 API 参考:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString()
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString()
回答by BobTheBuilder
Use:
利用:
System.out.println(Arrays.toString(MyArray));
In order to print the array elements. In your case you used the default Object.toString()implementation, which is not so informative...
为了打印数组元素。在您的情况下,您使用了默认的Object.toString()实现,这不是那么有用...
回答by durron597
Use this instead:
改用这个:
System.out.println(Arrays.toString(MyArray));
回答by Nikitin Mikhail
to print the array you need to use the loop. for example:
要打印您需要使用循环的数组。例如:
for (int i: MyArray){
System.out.print(i + " ")}