-2

i'm trying to simply create a method that returns a 2D array that has all the values entered based off a given pattern. Eclipse says that my code doesn't have any errors, yet when I go to run I get the response "[[I@2a139a55". I googled it and learned what it was about, yet I still don't know how to fix my code.

public class Transpose {

public static int[][] createPatterned2DArray(int row,int column){
    int width = column;
    int height = row;
    int[][] array = new int[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            array[i][j] = i + j + (width * i);
        }
    }

    return array;

}

public static void main(String[] args) {


    System.out.print(createPatterned2DArray(3,5));


}

}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
NeilnGauss
  • 13
  • 3
  • 3
    Use `Arrays.deepToString` – Eran Jun 01 '17 at 05:46
  • 1
    the output is correct, as an `array` is an `Object`. Here it uses the default `Object` implementation of `toString`, which is printing the `type@hascode ` of the `Object`. You either want to iterate over it or use some api to print it directly. – SomeJavaGuy Jun 01 '17 at 05:47

3 Answers3

5

Try

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

or if your array contains other arrays as elements

System.out.println(Arrays.deepToString(array))
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
pragadez
  • 89
  • 5
2

You will have to print the 2d array.

For that you cannot simply do System.out.println()

The code should be somewhat like the following:

public class Transpose {

    public static int[][] createPatterned2DArray(int row,int column){
        int width = column;
        int height = row;
        int[][] array = new int[height][width];

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                array[i][j] = i + j + (width * i);

            }
        }

        return array;

    }

    public static void main(String[] args) {


        int arr[][] = createPatterned2DArray(3,5);

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


    }

}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

This System.out.print(array) statement won't give you the expected output of array elements values:

Actually you will get like this strange output when you print the array:

[[I@15db9742

The reason for this output:

An array is treated as an object, so the default outcome of Object#toString() will be used as string representation.

To achieve what you want:

  • Use Arrays.toString() method to print simple array or one-dimensional array.
  • Use Arrays.deepToString()method to print nested array.
  • Or you can use for loop traditional way to print the array.

In your case to print the 2D array you can just use:

System.out.print(Arrays.deepToString(createPatterned2DArray(3,5)));

Check this answer for more information.

Oghli
  • 2,200
  • 1
  • 15
  • 37