简体   繁体   English

数组索引处的打印值返回哈希码

[英]Printing value at array index returns hashcode

Just playing around with displaying values in a two dimensional array and noticed my code prints some hashcodes and then the values. 只是在二维数组中显示值,并注意到我的代码打印了一些hashcodes码,然后是值。 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. 但我不打印数组对象本身(如完成后这里 ),或至少显式调用toStringhashcode的数组对象的方法。 Instead, I'm directly accessing and printing the values in the array using arrayObj[i] and arrayObj[i][j] . 相反,我使用arrayObj[i]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. 似乎我的代码以某种方式调用数组的toStringhashcode方法。 Thoughts? 思考? How can I modify to print just the values? 如何修改以仅打印值?

javac and java version 1.8.0 javac和java版本1.8.0

A two dimensional array is an array of arrays. 二维数组是数组的数组。 The array you create ( int[2][4] ) looks like this 你创建的数组( int[2][4] )看起来像这样

[ 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) . 要打印数组的值,可以使用Arrays.toString(arr) In this case you can omit the inner loop, because Arrays.toString() will do it for you. 在这种情况下,您可以省略内部循环,因为Arrays.toString()将为您完成。

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

The hash values come from your first System.print sentence. 哈希值来自您的第一个System.print语句。

System.out.print(arrayObj[i] + " "); System.out.print(arrayObj [i] +“”);

With this sentence you are printing an Object (an array) and therefore Java is invoking the default toString method. 使用此句子,您将打印一个Object(数组),因此Java正在调用默认的toString方法。

When you print 当你打印

arrayObj[i]

you get the default Object toString() from an array. 从数组中获取默认的Object toString() You could use Arrays.toString(int[]) to make that a String like 您可以使用Arrays.toString(int[])来使它像String一样

System.out.println(Arrays.toString(arrayObj[i]));

Alternatively, you can use Arrays.deepToString(Object[]) to print your multi-dimensional array without a loop 或者,您可以使用Arrays.deepToString(Object[])在没有循环的情况下打印多维数组

System.out.println(Arrays.deepToString(arrayObj));

Edit 编辑

You could use formatted output to build your table. 您可以使用格式化输出来构建表。

public static void printArray(int[][] arrayObj) {
    for (int i = 0; i < arrayObj.length; i++) {
        System.out.printf("%03d: ", i + 1);
        for (int val : arrayObj[i]) {
            System.out.printf("% 4d ", val);
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    int[][] table = { { 3, 1, 2 }, { 5, 1, 4 }, { 100, 200, 300 } };
    printArray(table);
}

Output is 输出是

001:    3    1    2 
002:    5    1    4 
003:  100  200  300 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM