【问题标题】:Matrix cannot be resolved to a variable矩阵无法解析为变量
【发布时间】:2014-07-19 03:38:13
【问题描述】:
import java.util.*;


public class Algorithm {

public class Matrix{     
    private Double[][] x;
}

public static Scanner scan = new Scanner(System.in);
private static String str;

public static void read_data(Double[] degrees, Double[] l) {
    l[0] = 0.0;
    int i;
    for (i = 0; i < 9; i++) {
        str = scan.next();  //passem la primera columna
        str = scan.next();  //agafem el valor del desplaçament
        str = str.substring(0, str.length()-1); //traiem la coma
        l[i+1] = Double.parseDouble(str);

        str = scan.next();  //passem la primera columna
        str = scan.next();  //agafem el valor del desplaçament
        str = str.substring(0, str.length()-1); //traiem la coma
        degrees[i] = Double.parseDouble(str);
    }
    degrees[i] = 0.0;
}

public static void init_Matrix(Double[][] M, int i, Double[] degrees, Double[] l) {

    M[0][3] = l[i];
    M[0][0] = Math.cos(degrees[i]);
    M[0][1] = -Math.sin(degrees[i]);
    M[1][0] = Math.sin(degrees[i]);
    M[1][1] = Math.cos(degrees[i]);

    for (int k = 0; i < 4; k++) {
        for (int j = 0; j < 4; j++) {
            if (k == j && (M[k][j] == null)) M[k][j] = 1.0;
            else if(M[k][j] == null) M[k][j] = 0.0;
        }
    }
}

public static void init_Ultima_Matrix(Double[][] M, int i, Double[] l) {
    M[0][3] = l[i];
    for (int k = 0; k < 4; k++) {
        for (int j = 0; j < 4; j++) {
            if (k == j) M[k][j] = 1.0;
            else if(M[k][j] == null) M[k][j] = 0.0;
        }
    }
}

public static void init_Derivada(Double[][] M, int i, Double[] degrees) {
    M[0][0] = -Math.sin(degrees[i]);
    M[0][1] = -Math.cos(degrees[i]);
    M[1][0] = Math.cos(degrees[i]);
    M[1][1] = -Math.sin(degrees[i]);

    for (int k = 0; k < 4; k++) {
        for (int j = 0; j < 4; j++) {
            if(M[k][j] == null) M[k][j] = 0.0;
        }
    }
}

public static void init_Ultima_Derivada(Double[][] M, int i) {
    for (int k = 0; k < 4; k++) {
        for (int j = 0; j < 4; j++) {
            M[k][j] = 0.0;
        }
    }
}

public static void fulfill_Ts(Matrix[] Ts, Double[] degrees, Double[] l) {
    int i;
    for (i = 0; i < 9; i++) {
        Ts[i].x = new Double[4][4];
        init_Matrix(Ts[i].x, i, degrees, l);
    }
    init_Ultima_Matrix(Ts[i].x, i, l);

}

public static void fulfill_Ds(Matrix[] Ds, Double[] degrees) {
    int i;
    for (i = 0; i < 9; i++) {
        Ds[i].x = new Double[4][4];
        init_Derivada(Ds[i].x, i, degrees);
    }
    init_Ultima_Derivada(Ds[i].x, i);
}

private static Double[][] product(Double[][] A, Double[][] B){  
    Double suma = 0.0;  
    Double result[][] = new Double[4][4];  
    for(int i = 0; i < 4; i++){  
        for(int j = 0; j < 4; j++){  
            suma = 0.0;  
            for(int k = 0; k < 4; k++){  
                suma += A[i][k] * B[k][j];  
            }  
            result[i][j] = suma;  
        }  
    }  
    return result;  
}

private static void calc_Jacobian(Matrix[] Ts, Matrix[] Ds, int i, Double[][] jacobian) {
    Double[][] tmp;
    if (i == 0) tmp = Ds[0].x;
    else tmp = Ts[0].x;
    for (int j = 1; j < 10; j++) {
        if (j == i) tmp = product(tmp, Ds[j].x);
        else tmp = product(tmp, Ts[j].x);
    }
    jacobian[0][i] = tmp[0][3];
    jacobian[1][i] = tmp[1][3];
    jacobian[2][i] = tmp[0][0];
}

public static void main(String[] args) {
    Matrix[] Ts = new Matrix[10];
    Matrix[] Ds = new Matrix[10];
    for (int i = 0; i < 10; i++) {
        Ts[i].x = new Double[4][4];
        Ds[i].x = new Double[4][4];
    }
    Double[] degrees = new Double[10];
    Double[] l = new Double[10];
    read_data(degrees, l);
    fulfill_Ts(Ts, degrees, l);
    fulfill_Ds(Ds, degrees);

    Matrix jacobian = new Matrix();
    jacobian.x = new Double[3][9];
    for (int j=0; j<9; j++)
        calc_Jacobian(Ts, Ds, j, jacobian.x);
    //La matriu Jacobiana hauria d'estar acabada
}

}

嗯,这是我的代码。错误出现在第一行,其中显示“Matrix jacobian = new Matrix();”。我声明错了吗? Matrix的定义在代码的开头。 它说:没有封闭的算法类型的实例是可访问的。必须使用算法类型的封闭实例来限定分配(例如 x.new A(),其中 x 是算法的实例)。

此外,我得到一个例外:“Matrix[] Ts = new Matrix[10];”。我不能声明一个元素数组 Matrix 吗?

非常感谢。

【问题讨论】:

    标签: java arrays matrix declaration


    【解决方案1】:

    是的,试试这个:Matrix M = new Matrix();

    这个Matrix[] Ts = new Matrix[10]; 是有效的,但您还应该循环遍历数组元素并通过调用构造函数来初始化它们。否则,它们将保持null 值。

    【讨论】:

    • Matrix M = new Matrix(); 不是来自 main 方法的静态上下文。在这种情况下,我们需要显式的外部对象来创建内部类的实例。我们还可以将Matrix 移出Algorithm 类或使其成为静态。
    • 我已经修改了它,它也不起作用,彼得。我也编辑了帖子
    【解决方案2】:

    改变

    public class Matrix
    

    public static class Matrix
    

    创建没有 static 修饰符的嵌套类会创建一个闭包,它允许您引用封闭实例的非静态成员。如果你不声明它static,你需要一个封闭的实例(即this)来创建它,你在static void main() 中没有。

    解释一个闭包超出了这个答案的范围,但它将是Matrix 中的一个私有成员Algorithm _closure,当你提到一个非静态时,它被隐式引用(与this 相同) Algorithm的成员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2021-12-21
      • 2012-02-29
      • 2020-12-07
      • 2019-04-13
      • 1970-01-01
      相关资源
      最近更新 更多