Trying to get my matrix printed not the heap addre

2020-05-09 18:09发布

问题:

The code seems to run except what I am getting is not a matrix of a specified (by the user) size but what I think is a heap address Here's what it returns when the user inputs 2 for the size and then 4 numbers:

Enter matrix size: 2 Enter a 2 by 2 matrix row by row: 2 3 4 5 The row-sort matrix is...[[D@3c954549BUILD SUCCESSFUL (total time: 8 seconds)

here is the code....thank you in advance.

import java.util.Scanner;
public class Exercise7_26M {

   public static void main (String[]args)


   {
           //Prompt user for input of matrix size


       System.out.println("Enter matrix size: ");
    Scanner input = new Scanner(System.in);  
    int size = input.nextInt();
    double[][] m = new double [size][size];

    //prompt user for input of array
     System.out.print("Enter a " + size + " by  " + size + " matrix row by row: ");
     for (int row = 0; row < 2; row++)
           for (int column = 0; column < 2; column++)
               m[row][column] = input.nextDouble();

               System.out.print("The row-sort matrix is..." + m);


   }

回答1:

Java arrays do not override toString() so you are getting the default implementation from Object. Instead, you can use Arrays.deepToString(Object[]) like

System.out.println("The row-sort matrix is..." + Arrays.deepToString(m));


标签: java matrix heap