[英]How to print two 2D double arrays?
Like from {{1.0,2.0},{3.0,4.0}}
and {{0.1,0.2},{0.3,0.4}}
to {{1.1,2.2},{3.3,4.4}}
. 就像从
{{1.0,2.0},{3.0,4.0}}
和{{0.1,0.2},{0.3,0.4}}
到{{1.1,2.2},{3.3,4.4}}
。 If the two input arrays are different sizes, method should return null
. 如果两个输入数组的大小不同,则method应该返回
null
。
My code below shows [[D@6d06d69c
. 我的下面代码显示
[[D@6d06d69c
。
What is wrong with my code? 我的代码有什么问题?
public class Homework13_1 {
public static double[][] sum(double[][]a,double[][]b){
double[][] newA= new double[a.length][a[0].length];
int c=0;
if ((a.length==b.length)){
for (int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
newA[i][j]=a[i][j]+b[i][j];
}
}
return newA;
}
else{
return null;
}
}
public static void main(String[] args) {
double[][]x={{1,2,3},{2,3,4}};
double[][]y={{2,3,4},{1,1,1}};
double[][] b = {{1,2,-9,7},{3,4,9.9,1.2}};
System.out.println(sum(x, y));
}
}
Java arrays don't override toString
. Java数组不会覆盖
toString
。 But there is Arrays.deepToString(Object[])
which says (in part) This method is designed for converting multidimensional arrays to strings . 但是有
Arrays.deepToString(Object[])
表示(部分) 此方法旨在将多维数组转换为字符串 。 You could change 你可以改变
System.out.println(sum(x, y));
to 至
System.out.println(Arrays.deepToString(sum(x, y)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.