Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印阵列:内存地址还是其内容?_Java_Arrays_Vector - Fatal编程技术网

Java 打印阵列:内存地址还是其内容?

Java 打印阵列:内存地址还是其内容?,java,arrays,vector,Java,Arrays,Vector,你能解释一下为什么这个代码吗 int[] test={0,0,0,0,0}; System.out.println(test); Stack<Integer> stack = new Stack<Integer>(); stack.push(1); stack.push(3); stack.push(5); stack.push(2); stack.push(4); System.out.println(stack); 打印出类似于[I@42e816也许

你能解释一下为什么这个代码吗

int[] test={0,0,0,0,0};
System.out.println(test);
 Stack<Integer> stack = new Stack<Integer>();
 stack.push(1);
 stack.push(3);
 stack.push(5);
 stack.push(2);
 stack.push(4);
 System.out.println(stack);
打印出类似于[I@42e816也许是内存地址,但这段代码

int[] test={0,0,0,0,0};
System.out.println(test);
 Stack<Integer> stack = new Stack<Integer>();
 stack.push(1);
 stack.push(3);
 stack.push(5);
 stack.push(2);
 stack.push(4);
 System.out.println(stack);
打印[1,3,5,2,4]?有什么区别


如果堆栈派生自向量和数组中的向量,那么这种不同行为的原因是什么?

堆栈有一个重写的toString方法,该方法迭代每个元素并打印它们,因此您可以看到格式化的打印

您可以使用Arrays.toStringtest以指定的方式打印数组的内容。到目前为止,您正在对数组对象而不是其内容执行toString


您可以参考了解数组对象的默认toString实现,从而了解打印测试数组时注意到的输出。

堆栈有一个重写的toString方法,该方法遍历每个元素并打印它们,因此您可以看到格式化的打印

您可以使用Arrays.toStringtest以指定的方式打印数组的内容。到目前为止,您正在对数组对象而不是其内容执行toString


您可以参考了解数组对象的默认toString实现,从而了解打印测试数组时注意到的输出。

集合有一个定义良好的toString方法,但数组忘记了IMHO,并使用默认object.toString以及对象中的许多其他默认方法,这些方法不是很好也很有用。您需要调用后来添加的许多帮助器类中的一个,以使数组更有用

System.out.println(Arrays.toString(test));
十六进制是对象的默认哈希代码,而不是地址。它可能不是唯一的,即使数组在内存中移动,这个数字也不会改变

用于数组的帮助器类

java.lang.reflect.Array
java.util.Arrays
java.lang.System.arraycopy(); // one method
额外的

org.apache.commons.lang.ArrayUtils
org.uispec4j.utils.ArrayUtil
toxi.util.datatypes.ArrayUtil
net.sf.javaml.utils.ArrayUtils
com.liferay.portal.kernel.util.ArrayUtil
还有更多


不是说数组不应该有自己的方法,而是有太多的方法可供选择。

集合有一个定义良好的toString方法,但是数组忘记了IMHO,使用了默认的Object.toString以及许多其他的Object中的默认方法,这些方法也不是很有用。您需要调用一个后来添加了许多帮助器类,以使数组更有用

System.out.println(Arrays.toString(test));
十六进制是对象的默认哈希代码,而不是地址。它可能不是唯一的,即使数组在内存中移动,这个数字也不会改变

用于数组的帮助器类

java.lang.reflect.Array
java.util.Arrays
java.lang.System.arraycopy(); // one method
额外的

org.apache.commons.lang.ArrayUtils
org.uispec4j.utils.ArrayUtil
toxi.util.datatypes.ArrayUtil
net.sf.javaml.utils.ArrayUtils
com.liferay.portal.kernel.util.ArrayUtil
还有更多

不是说数组不应该有自己的方法,而是有太多的方法可供选择。

Stack>Vector>AbstractList>AbstractCollection

AbstractCollection定义toString,System.out.println调用toString来获取字符串表示形式

int[]不是这些对象类型之一-它是一个本机数组,因此它在对象类中使用toString的默认实现。

Stack>Vector>AbstractList>AbstractCollection

AbstractCollection定义toString,System.out.println调用toString来获取字符串表示形式


int[]不是这些对象类型之一-它是本机数组,因此在对象类中使用toString的默认实现。

数组是基元,因此打印它们会导致仅打印引用数组是基元,因此打印它们会导致仅打印引用