Brute force technique is returning some gibberish when applied on a 2D array

I am trying to solve a 2D array question using brute force, but when I run the code in my IDE it returns something in gibberish that I am not able to decode.

Question: https://leetcode.com/problems/flipping-an-image/

import java.util.*;
import java.io.*;

public class flippingAnImage {
    public static void main(String[] args){
        int[][] arr = {{1,1,0},{1,0,1},{0,0,0}};
        System.out.println(Arrays.toString(flip(arr)));
    }

    public static int[][] flip(int[][] image){
        for(int i = 0 ; i < image.length ; i++){
            for(int j = image[i].length-1,k=0 ; j>=0 && k < image[i].length;j--,k++){
                image[i][k] = image[i][j];
            }
        }
        return image;
    }

}

This is what it returns: [[I@5acf9800, [I@4617c264, [I@36baf30c]

Any help would be appreciated here


Solution 1:

toString() accepts one dimensional array as input argument and returns a string representation of it. It doesn't work for multi-dimensional arrays. Just replace toString with deepToString() for multi-dimensional array instead.

Like,

import java.util.Arrays;

public class ArrayFlipper {
    public static void main(String[] args){
        int[][] arr = {{1,1,0},{1,0,1},{0,0,0}};
        System.out.println(Arrays.deepToString(flip(arr)));
    }

    public static int[][] flip(int[][] image){
        for(int i = 0 ; i < image.length ; i++){
            for(int j = image[i].length-1,k=0 ; j>=0 && k < image[i].length;j--,k++){
                image[i][k] = image[i][j];
            }
        }
        return image;
    }

}

This would return output:

[[0, 1, 0], [1, 0, 1], [0, 0, 0]]

If your code still doesn't give the intended output, think about why it's printing the current result.

Solution 2:

Because arr is a two dimension array. And what you print is the address of the rows about the arr. (Just as user16320675 mentioned, the address of the rows is not correct here and it should be the hashcode of the rows) In java API(https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html) where you can find that there is no toString(int[][] a) but toString(int[] a). Your code can modify to get the value.

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

But why is not the answer in leetcode. Your algorithm just flip but not invert. So you should think more about the algorithm you design.