0

I have a matrix homework. When I run the program, in the console It always returns by the memory address and not by the values. I am European, I used the German locale. I've thought that maybe the localization is the problem, so I've changed to US, but It doesn't solves my problem.

In console:

[[D@459189e1, [D@55f33675]
[[D@527c6768, [D@65690726]

Here my code:

import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;


public class Inverse2x2Matrix {

    public static double[][] inverse2x2Matrix(double[][] A) {
        double det = A[0][0] * A[1][1] - A[0][1] * A[1][0];
        double m00 = -1 * A[1][1] / det;
        double m01 = A[0][1] / det;
        double m10 = A[1][0] / det;
        double m11 = -1 * A[0][0] / det;
        double[][] B = { { m00, m01 }, { m10, m11 } };
        return B;
    }

    public static void main(String[] args) {

        Locale.setDefault(Locale.US);

    System.out.println("Enter a, b, c, d: 0");
    Scanner sc = new Scanner(System.in);
    String input=sc.next();
    sc.close();

    double[][] A = new double[2][2];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            A[i][j] = Double.parseDouble(input);
        }
    }

        System.out.println(Arrays.toString(A));
        System.out.println(Arrays.toString(inverse2x2Matrix(A)));
    }
        double[][] C = {{1.0,2.0},{3.0,4.0}};
    System.out.println(Arrays.toString(C));
    System.out.println(Arrays.toString(inverse2x2Matrix(C)));

}

2 Answers 2

3

Use Arrays.deepToString to pretty-print the contents of an array recursively. Else, every element of the array is printed using its toString method, which indeed prints its type followed by its hashCode.

0
2

A is array of array, so when you type Arrays.toString(A) it takes every inner array of A and call toString on it, which returns this address-like representation. You need to iterate through A manually:

for (double[] ar : A) {
    System.out.println(Arrays.toString(ar));
}

Or write helper method:

private static void printArray(double[][] ar) {
    for (double[] inner : ar) {
        System.out.println(Arrays.toString(inner));
    }
}

UPDATE

JB Nizet's advice is much better.

1
  • Thank you, your answer is explain why I had problem :)
    – blaces
    Oct 1, 2011 at 17:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.