问题描述
我是编程新手。我确定这个问题的答案就在那里,但我不知道该搜索什么。
I'm new to programming. I'm sure the answer for this question is out there, but I have no idea what to search for.
好的,我会直截了当。
这是我的代码:
int[] arr;
arr = new int[5];
arr[0] = 20;
arr[1] = 50;
arr[2] = 40;
arr[3] = 60;
arr[4] = 100;
System.out.println(arr);
这个编译并正常工作。这只是CMD的输出,我很头晕。
This compiles and works fine. It's just the output from CMD that I'm dizzy about.
这是输出: [I @ 3e25a5
。
我希望输出代表列表中完全相同的数字( arr
)。我该怎么做?
I want the output to represent the exact same numbers from the list (arr
) instead. How do I make that happen?
推荐答案
每个对象都有一个 toString()
方法,默认方法是显示对象的类名表示,然后是 @
,后跟哈希码。所以你看到的是 int
数组的默认 toString()
表示。要打印数组中的数据,您可以使用:
Every object has a toString()
method, and the default method is to display the object's class name representation, then @
followed by its hashcode. So what you're seeing is the default toString()
representation of an int
array. To print the data in the array, you can use:
System.out.println(java.util.Arrays.toString(arr));
或者,您可以使用循环遍历数组
循环正如其他人在此主题中发布的那样。
Or, you can loop through the array with a for
loop as others have posted in this thread.
这篇关于Java数组打印出奇怪的数字和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!