返回数组的内存分配,而不是Java值 [英] Array returning memory allocation instead of value in Java
问题描述
考虑一个简单的情况下,像这样的:
Consider a simple case like this one :
public static void array()
{
String myString = "STACKOVERFLOW";
int [] checkVal = new int[myString.length()];
for(int i=0; i<myString.length();i++){
checkVal[i] = (int)myString.charAt(i);
}
System.out.println(checkVal[0]);
System.out.println(checkVal[1]);
System.out.println(checkVal[2]);
System.out.println(checkVal[3]);
System.out.println(checkVal[4]);
System.out.println(checkVal);
}
这将输出以下内容:
83
84
65
67
75
[I@fc9944
能否有人请解释这样对我?我怎样才能从数组,而不是它获取正确的信息的内存分配?
Can some one please explain this to me ? How can I retrieve the proper information from the Array instead of it's memory allocation ?
推荐答案
如果您希望它打印$ $ p ptty,使用 Arrays.toString
来做到这一点:
If you want it pretty printed, use Arrays.toString
to do it:
System.out.println(Arrays.toString(checkVal);
如果不是,它只是打印出的toString
阵列,这是从对象继承了
,和生成的字符串
包含它的类型和散列code:
If not, it will just print out the toString
of the array, which is inherited from Object
, and generated a String
contains its type and the hash code:
Object类的toString方法返回一个包含其中的对象是一个实例,该符号字符'@'的类名称的字符串,散列$ C的无符号十六进制再presentation对象$ C。换句话说,该方法返回字符串等于的值:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
的getClass()。的getName()+'@'+ Integer.toHexString(哈希code())
getClass().getName() + '@' + Integer.toHexString(hashCode())
<一个href=\"http://stackoverflow.com/questions/7060016/why-does-the-tostring-method-in-java-not-seem-to-work\">This答案也有相关的信息。
This answer also has relevant info.
这篇关于返回数组的内存分配,而不是Java值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!