数组索引处的打印值返回哈希码(Printing value at array index returns hashcode)

编程入门 行业动态 更新时间:2024-10-25 18:30:26
数组索引处的打印值返回哈希码(Printing value at array index returns hashcode) java

只是在二维数组中显示值,并注意到我的代码打印了一些hashcodes码,然后是值。 但我不打印数组对象本身(如此处的帖子所做)或至少显式调用数组对象的toString或hashcode方法。 相反,我使用arrayObj[i]和arrayObj[i][j]直接访问和打印数组中的值。

这是我的代码:

class PrintTwoDimArray { public int [][] createArray () { int counter = 0; int[][] intArray = new int [2][4]; for (int i = 0; i < intArray.length; i++) { for (int j = 0; j < intArray[i].length; j++) { intArray[i][j] = ++counter; } } return intArray; } public void printArray ( int [][] arrayObj ) { for (int i = 0; i < arrayObj.length; i++) { System.out.print(arrayObj[i] + " "); for (int j = 0; j < arrayObj[i].length; j++) { System.out.print(arrayObj[i][j] + " "); } System.out.println(); } } } class TestPrintTwoDimArray { public static void main (String[] args) { PrintTwoDimArray twoDim = new PrintTwoDimArray(); int [][] multiArray = new int [2][4]; multiArray = twoDim.createArray(); twoDim.printArray(multiArray); } }

我的输出如下:

似乎我的代码以某种方式调用数组的toString或hashcode方法。 思考? 如何修改以仅打印值?

javac和java版本1.8.0

Just playing around with displaying values in a two dimensional array and noticed my code prints some hashcodes and then the values. But I am not printing the array object itself (as done in the post here) or at least explicitly calling the toString or hashcode method of the array object. Instead, I'm directly accessing and printing the values in the array using arrayObj[i] and arrayObj[i][j].

Here's my code:

class PrintTwoDimArray { public int [][] createArray () { int counter = 0; int[][] intArray = new int [2][4]; for (int i = 0; i < intArray.length; i++) { for (int j = 0; j < intArray[i].length; j++) { intArray[i][j] = ++counter; } } return intArray; } public void printArray ( int [][] arrayObj ) { for (int i = 0; i < arrayObj.length; i++) { System.out.print(arrayObj[i] + " "); for (int j = 0; j < arrayObj[i].length; j++) { System.out.print(arrayObj[i][j] + " "); } System.out.println(); } } } class TestPrintTwoDimArray { public static void main (String[] args) { PrintTwoDimArray twoDim = new PrintTwoDimArray(); int [][] multiArray = new int [2][4]; multiArray = twoDim.createArray(); twoDim.printArray(multiArray); } }

My output is as follows:

It seems that my code is somehow calling the toString or hashcode method of the array. Thoughts? How can I modify to print just the values?

javac and java version 1.8.0

最满意答案

二维数组是数组的数组。 你创建的数组( int[2][4] )看起来像这样

[ 0 ] -> [ 1, 2, 3, 4 ] [ 1 ] -> [ 5, 6, 7, 8 ]

因此,当您只访问第一个维度时,您将获得包含第二个维度的数组。

int[][] arr = createArray(); System.out.println(arr[0]);

会输出类似的东西

[I@1db9742

要打印数组的值,可以使用Arrays.toString(arr) 。 在这种情况下,您可以省略内部循环,因为Arrays.toString()将为您执行此操作。

public void printArray ( int [][] arrayObj ) { for (int i = 0; i < arrayObj.length; i++) { System.out.println(Arrays.toString(arrayObj[i])); } }

A two dimensional array is an array of arrays. The array you create (int[2][4]) looks like this

[ 0 ] -> [ 1, 2, 3, 4 ] [ 1 ] -> [ 5, 6, 7, 8 ]

So when you only access the first dimension you will get the array that holds the second dimension.

int[][] arr = createArray(); System.out.println(arr[0]);

will output something like

[I@1db9742

To print an array's values you can use Arrays.toString(arr). In this case you can omit the inner loop, because Arrays.toString() will do it for you.

public void printArray ( int [][] arrayObj ) { for (int i = 0; i < arrayObj.length; i++) { System.out.println(Arrays.toString(arrayObj[i])); } }

更多推荐

本文发布于:2023-08-07 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   哈希码   Printing   hashcode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!