为什么打印的Java阵列显示的存储器位置 [英] Why does printing a Java array show a memory location
问题描述
int[] answer= new int[map.size()];
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int j=0; j<answer.length;j++){
int x=map.get(keys.get(j));
answer[j]=x;
}
return answer
当我打印 X
使用的System.out.println(X)
在循环中,我得到的值 1,2,3
但是当我返回答案,并打印出来,我得到 [I @ 9826ac5
。任何想法,为什么?
When I print x
using System.out.println(x)
in the loop, I get values of 1, 2, 3
but when I return the answer and print it, I get [I@9826ac5
. Any idea why?
推荐答案
I [
是那种类类型为整型数组。打印出这个数组本身将打印类的类型 @
再短的十六进制字符串,因为这是数组的哈希值code。这是因为一些你可能已经看到像对象@ 0b1ac20相同
。这是作为默认<一href=\"http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString%28%29\"><$c$c>toString()$c$c>为对象
。
I[
is kind of the "class type" for an array of integer. Printing out this array itself will print the class type @
then a short hex string because that's the hash code of the array. It's the same as something you've probably seen like Object@0b1ac20
. This is implemented as the default toString()
for Object
.
也许你想返回数组的特定元素或使用for循环打印整个数组?
Maybe you want to return a specific element of the array or print the whole array using a for loop?
这篇关于为什么打印的Java阵列显示的存储器位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!