矩阵的元素是内存地址而不是元素的值
我有一份矩阵作业。当我运行程序时,在控制台中它总是按内存地址返回,而不是按值返回。 我是欧洲人,我使用德国语言环境。我想也许是本地化的问题,所以我改成了美国,但这并不能解决我的问题。
在控制台中:
[[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
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 itstoString
method, which indeed prints its type followed by its hashCode.A
是数组的数组,因此当您输入Arrays.toString(A)
时,它会获取A
的每个内部数组并调用toString
,它返回这个类似地址的表示。您需要手动迭代A
:或者编写辅助方法:
UPDATE
JB Nizet 的建议要好得多。
A
is array of array, so when you typeArrays.toString(A)
it takes every inner array ofA
and calltoString
on it, which returns this address-like representation. You need to iterate throughA
manually:Or write helper method:
UPDATE
JB Nizet's advice is much better.