Sayan Banerjee Sayan Banerjee - 20 days ago 7
Java Question

Type mismatch error in Java, 2D array declaration

public HashMap<String, double[][]> GradientDescent(double[][] ratingData_norm, double[][] ratingData_binary, double[][] theta, double[][] X, int iter, double alpha, double lambda)

{
HashMap<String, double[][]> result= new HashMap<String, double[][]>();

double[][] theta_tran=new *double[theta[0].length][theta.length]();*
double[][] theta_grad=new *double[theta.length][theta[0].length]();*
-------
---
--
--}


I am getting error in declaration of the 2D double array.
Type mismatch error. can't convert double[][][] to double[][]
can anyone kindly tell me where I am getting it wrong and how I can fix it?


Many thanks in advance!!!!

Answer

I fixed the code you posted so I can try to compile it.

import java.util.*;

public class Test {

    public HashMap<String, double[][]> GradientDescent(double[][] ratingData_norm, double[][] ratingData_binary, double[][] theta, double[][] X, int iter, double alpha, double lambda)
    {
        HashMap<String, double[][]> result= new HashMap<String, double[][]>();

        double[][] theta_tran=new double[theta[0].length][theta.length]();
        double[][] theta_grad=new double[theta.length][theta[0].length]();
    }

}

Then I compiled it:

> javac -d . Test.java
Test.java:9: error: ';' expected
        double[][] theta_tran=new double[theta[0].length][theta.length]();
                                                                       ^
Test.java:10: error: ';' expected
        double[][] theta_grad=new double[theta.length][theta[0].length]();
                                                                       ^
2 errors

For the next time you ask a question, you should post minimal amount of code that is ready to be compiled, and can demonstrate your problems.

You should not modify your code in any strange way to indicate where your problem is. We have no idea if those asterisks are actual part of your code or not. If you must add something, use comments.

And you should have included the error message you are getting. The error message made it pretty clear what you are doing wrong.

This can be compiled:

import java.util.*;

public class Test {

    public HashMap<String, double[][]> GradientDescent(double[][] ratingData_norm, double[][] ratingData_binary, double[][] theta, double[][] X, int iter, double alpha, double lambda)
    {
        HashMap<String, double[][]> result= new HashMap<String, double[][]>();

        // No brackets needed for array instantiation
        double[][] theta_tran=new double[theta[0].length][theta.length];
        double[][] theta_grad=new double[theta.length][theta[0].length];

        return null;
    }

}

And that's not the error you described at all. If you really have a type mismatch, it's not anywhere in the code you posted.