0

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

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
Harsh Sharma
  • 33
  • 1
  • 3
  • 1
    This is not garbage. I can't say why you are seeing this output, but this is a certain type of encoding https://stackoverflow.com/a/3442100/5744883 – Parnab Sanyal Jan 15 '22 at 08:08
  • 3
    https://stackoverflow.com/a/409795/5744883. You need to use `deepToString`. as you are printing nested array. Now look at your previous output. It is saying that, each element is an array. `[` is the encoding for array. – Parnab Sanyal Jan 15 '22 at 08:13
  • 2
    [Java - Best way to print 2D array](https://stackoverflow.com/questions/19648240/java-best-way-to-print-2d-array) – Abra Jan 15 '22 at 08:14

2 Answers2

2

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.

1

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.

Justin Shi
  • 33
  • 6
  • 3
    there is no "address of the rows" being printed - it is just the hash code (as specified in the [`Object.toString` method](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#toString()): "... `getClass().getName() + '@' + Integer.toHexString(hashCode())` ..." {the hash code may be *based* on the address, but it is not the address} {why call `Arrays.toString(arr[i]);` (first call) without ever using the result?} – user16320675 Jan 15 '22 at 09:37