0

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}}. If the two input arrays are different sizes, method should return null.

My code below shows [[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));


    }

}
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Xiaolong
  • 79
  • 2
  • 10

1 Answers1

0

Java arrays don't override toString. But there is Arrays.deepToString(Object[]) which says (in part) This method is designed for converting multidimensional arrays to strings. You could change

System.out.println(sum(x, y));

to

System.out.println(Arrays.deepToString(sum(x, y)));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249