英文:
JAVA printing correct output with an error [[I@6acbcfc0 in MATRIX-ADDITION
问题
我使用二维数组编写了一个用于添加两个矩阵的代码。但在运行后,它显示了一个带有错误[[I@6acbcfc0
的结果。如果您知道[[I@6acbcfc0
的含义,也请进行描述。以下是代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter dimensions: ");
int rows = sc.nextInt();
int cols = sc.nextInt();
int a[][] = new int[rows][cols];
int b[][] = new int[rows][cols];
System.out.println("Enter first matrix: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = sc.nextInt();
}
}
int c[][] = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Result is: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
输出为:
Enter dimensions:
2 2
Enter first matrix:
1 2
1 2
Enter second matrix:
1 2
1 2
Result is:
2 4
2 4
请帮我去除[[I@6acbcfc0
,如果您在我的代码中发现任何错误,请纠正,这样我就可以更好地理解。谢谢。
英文:
I wrote code for adding two matrices with the help of two-dimensional arrays. But after running, it is showing a result with error [[I@6acbcfc0
. If you know the meaning of [[I@6acbcfc0
, then please describe it as well. Following is the code:
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter dimensions: ");
int rows = sc.nextInt();
int cols = sc.nextInt();
int a[][] = new int [rows][cols];
int b[][] = new int [rows][cols];
System.out.println("Enter first matrix: ");
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix: ");
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
b[i][j] = sc.nextInt();
}
}
int c [][] = new int [rows][cols];
for (int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Result is " + c);
for(int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
and the output is
Enter dimensions:
2 2
Enter first matrix:
1 2
1 2
Enter second matrix:
1 2
1 2
Result is [[I@6acbcfc0
2 4
2 4
Please help me in removing "[[I@6acbcfc" and if you find any mistake in my code, please correct it so that i can understand it further better. Thank you.
答案1
得分: 0
你正在使用System.out.println("Result is " + c);
打印c,所以它被添加到输出中。只需移除那部分代码,你就可以继续了。
你还可以尝试使用以下代码来打印二维数组:
System.out.println(Arrays.deepToString(c));
对于打印普通数组:
System.out.println(Arrays.toString(c));
英文:
You are printing c using System.out.println("Result is " + c);
so it is being added to output. Just remove that and you are good to go.
You can also try this to print 2D array.
System.out.println(Arrays.deepToString(c));
for printing normal array
System.out.println(Arrays.toString(c));
答案2
得分: 0
你的问题在于你正在执行以下代码:
System.out.println("Result is " + c);
其中c是int [][]
你可能希望整个数组被打印出来,但实际上并不存在这样的行为。
你看到打印出来的结果是c.toString()
的结果,但这对你来说没有什么用。
如果你希望打印出数组的内容,你需要编写代码逐个元素地打印出来。实际上,在下面你已经这样做了。
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
因此,只需从上面的println
调用中删除+ c
即可。
英文:
Your problem is that you are executing
System.out.println("Result is " + c);
where c is int [][]
Presumably you want the entire array to be printed out by thus, but this is not behavior that actually exists.
What you see printed is the result of c.toString()
, which is not defined to be anything useful to you.
If you want the array content printed out, you'll have to write code to print it, element by element. Which, in fact, you have already done, immediately below.
for(int i = 0; i<rows; i++) {
for (int j = 0; j<cols; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
So just remove the + c
from the preceding println
call.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论