0

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(" ");
    }      
 }

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

This was very helpful. However my end result that I am trying to end up with is: {{ 3, 2},{ 1, 1}} and with the arrays I seem to be getting [3.0, 2.0] , [1.0, 1.0]. Is there any way of changing this?
@Will This is the default format of Arrays.toString. If you want a different format, you can iterate over the arrays and print them however you like.
1

#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);
    }//...

5 Comments

Thanks for the follow up although I am struggling to implement it... Is this correct? I have added the code to your answer as too hard to read otherwise
Also my method public static double[][] multiplyMatrix(double[][] A, double[][] B) is meant to have a return. It used to be C, now what am I meant to return as this prints the string already?
Ok, I think I am missing something here. I have added the code to yours to view it better and have put the output I am getting.
I don't follow what you said, if I take: String finalString = Arrays.deepToString(C) .replace("[", "{") .replace("]", "}"); and put it outside of the method how would that work? There would be no variable and the test class would only call the method not what follows...? Im sorry Im still a bit lost here
Thanks for keeping on trying. The problem is the String has to be changed within the method. It will be passed through a test file and the test class will only call the method - the answer has to be displayed exactly how they want it afterwards. Any ideas?
0

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.

Comments

0

[[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,

Comments

0

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, e.g. filtering some arrays or change the way display them, you can have nested loops.

1 Comment

I tried using this code but I now get the following: ---- Test Multiply Matrix ---- [[D@7f31245a, [D@6d6f6e28] Should return C={{ 3, 2},{ 1, 1}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.