矩阵的元素是内存地址而不是元素的值

发布于 2024-12-07 17:15:44 字数 1376 浏览 0 评论 0原文

我有一份矩阵作业。当我运行程序时,在控制台中它总是按内存地址返回,而不是按值返回。 我是欧洲人,我使用德国语言环境。我想也许是本地化的问题,所以我改成了美国,但这并不能解决我的问题。

在控制台中:

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

这是我的代码:

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)));

}

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)));

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-12-14 17:15:44

使用 Arrays.deepToString 以递归方式漂亮地打印数组的内容。否则,数组的每个元素都使用其 toString 方法打印,该方法确实打印其类型,后跟其 hashCode。

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.

我家小可爱 2024-12-14 17:15:44

A 是数组的数组,因此当您输入 Arrays.toString(A) 时,它会获取 A 的每个内部数组并调用 toString ,它返回这个类似地址的表示。您需要手动迭代 A

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

或者编写辅助方法:

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

UPDATE

JB Nizet 的建议要好得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文