'Why does Java give this output? [duplicate]

When trying to run the following code:

public static void main(String[] args) {
    int num_amount = 7;
    int[] num_array = {3, 4, 1, 1, 2, 12, 1};
    System.out.println(num_array.length);
    for (int i = 0; i < num_array.length; i++) {
        num_array[i] = 0;
        System.out.println(num_array);
    }
}

This is the output i get:

7
[I@ea30797
[I@ea30797
[I@ea30797
[I@ea30797
[I@ea30797
[I@ea30797
[I@ea30797

Has anyone ever experienced this "unusual" result? If so, what solution did you try out? Thanks in advance.



Solution 1:[1]

If you want to print the contents of an array you can use Arrays.toString.

System.out.println(Arrays.toString(num_array));

Solution 2:[2]

In Java, you do not print an array by placing the array name inside System.out.println();.

Here, you should use System.out.println(Arrays.toString(num_array));

See here for how to print an array in Java: https://stackoverflow.com/a/409795/18449247 (you should use .toString here)

Solution 3:[3]

You can try

System.out.println(num_array[i]);

If you want to print each element in new line..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anton Belev
Solution 2
Solution 3 Prashant