简体   繁体   中英

Java program output strange result

I am having an output problem with my java code. I am trying to implement this multiply matrix method and it compiles just fine. The only problem is my output. I seem to be getting the following:

  ---- Test Multiply Matrix ----
[[D@7f31245a 
Should return C={{ 3, 2},{ 1, 1}}

Can someone please help me understand where I am going wrong here. Thanks! Here is my source code:

public class Recommendation 
{
public static double[][] multiplyMatrix(double[][] A, double[][] B)
{
    int aRows = A.length;
    int bRows = B.length;
    int aColumns = A[0].length;
    int bColumns = B[0].length;

    if((aColumns != bRows))
    {
        return null;
     }
    else
    {
        double[][] C = new double[aRows][bColumns];
        for (int i = 0; i < 2; i++) 
        {
            for (int j = 0; j < 2; j++) 
            {
                C[i][j] = 0;
            }
        }

        for (int i = 0; i < aRows; i++) 
        {
            for (int j = 0; j < bColumns; j++) 
            { 
                for (int k = 0; k < aColumns; k++) 
                {
                C[i][j] += A[i][k] * B[k][j];
                }
            }
        }
        return C;
    }
}
static double [][] A =  {{ 1, 0, 2},
                        {  0, 1, 1}};
static double [][] B =  {{1, 2},
                        { 0, 1},
                        { 1, 0}};

    public static void main(String[] argss)
    {
    // TEST multiplyMatrix      
    System.out.println(" ---- Test Multiply Matrix ---- ");
    System.out.println(multiplyMatrix(A,B)); // should return C={{ 3, 2},{ 1, 1}}
    System.out.println("Should return C={{ 3, 2},{ 1, 1}}");
    System.out.println(" ");
    }      
 }

You might want to use Arrays.toString from java.util.Arrays to print arrays.

Or, if you want your output to be a little more custom, you can iterator over the array.

Note that Arrays.toString alone won't help you, since your array is two dimensional.

It would still print something of the form : [[I@355d56d5, [I@2efd552, [I@4f9dfbff]

Instead, you can do something like this :

double[][] C = multiplyMatrix(A,B);
for (double[] subArray : C) {
   System.out.print (Arrays.toString (subArray));
   System.out.print (" , ");
}
System.out.println();

Or, you can use Arrays.deepToString(C) which will take care of the hierarchy for you.

#deepToString Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.

You should use java.util.Arrays.deepToString(array) for multi-dimensional array.Currently you are printing Object reference's String representation.


You can use #replace method to replace [] with {}

//...
public static void main(String[] argss){
    // TEST multiplyMatrix      
    System.out.println(" ---- Test Multiply Matrix ---- ");
    double array[][] = multiplyMatrix(A,B);
    String finalString = Arrays.deepToString(array)
                               .replace("[", "{")
                               .replace("]", "}");
    System.out.println(finalString);
    }//...

public static double[][] multiplyMatrix(double[][] A, double[][] B) . Here you are returning a double array. which is not a primitive type. So, the default toString() method of array will be used (Which prints classname@hashCode , hence the output). You have to use Arrays.toString() to print the values properly.

You can use Arrays#deepToString() :

System.out.println(Arrays.deepToString(multiplyMatrix(A,B)));

multiplyMatrix returns an array , which is an object, and in Java since each object has toString() method, the default is displaying the class name representation, then adding @ sign and then the hashcode.

In order to better understand what's happening here, see the implementation of Arrays.deepToString .

Note that if you want more control on the output, eg filtering some arrays or change the way display them, you can have nested loops.

[[D@7f31245a means a 2D array of Double 's followed by the hashcode of the actual object.

Your multiplyMatrix() method returns exactly this, but the toString() method invoked is that on Object which prints exactly this. You'll need to to use methods on Arrays class to prettyprint arrays.

Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM