[英]Random Integer filling an array is not producing only integers
I have code to fill an array with 10 integers but for some reason it is not only filling it with integers. 我有代码用10个整数填充数组但由于某种原因它不仅用整数填充它。 EX.
EX。 [I@5483cd or [I@1690726 Anyone know why?
[我@ 5483cd或[我@ 1690726任何人都知道为什么?
public static void main(String[] args) {
int[] array = new int[10];
int length = array.length;
for (int i = 0; i < length; i++) {
array[i] = (int)(Math.random () * 10);
}
System.out.println(array);
}
System.out.println(array)
is printing the hashcode for the array, not the values inside the array. System.out.println(array)
正在打印System.out.println(array)
的哈希码,而不是数组内的值。 Iterate over the values in the array and print those, like this: 迭代数组中的值并打印出来,如下所示:
for (int val : array)
{
System.out.println(val);
}
Even simpler, use: Arrays.toString(array)
. 更简单,使用:
Arrays.toString(array)
。 See What's the simplest way to print a Java array? 请参阅打印Java数组最简单的方法是什么?
As other answers say, println is printing hashcode of the array. 正如其他答案所说,println正在打印数组的哈希码 。 If you want to see array's contents, you can iterate over every element and print it OR you can use Arrays.toString() methods.
如果要查看数组的内容,可以遍历每个元素并打印它,也可以使用Arrays.toString()方法。
import java.util.Arrays;
System.out.println(Arrays.toString(array));
If you have multidimensional arrays, use Arrays.deepToString() . 如果您有多维数组,请使用Arrays.deepToString() 。 Explore Arrays class for more such utility methods.
探索Arrays类以获取更多此类实用程序方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.