一旦我退出函数(Java),对象的值就为null import java.util.*; 公共类算法{ 公共静态类矩阵{ 私人双[]x; } 公共静态扫描仪扫描=新扫描仪(System.in); 私有静态字符串str; 公共静态无效读取数据(双[]弧度,双[]l){ l[0]=0.0; int i; 对于(i=0;i
这就是问题所在:一旦我退出函数(Java),对象的值就为null import java.util.*; 公共类算法{ 公共静态类矩阵{ 私人双[]x; } 公共静态扫描仪扫描=新扫描仪(System.in); 私有静态字符串str; 公共静态无效读取数据(双[]弧度,双[]l){ l[0]=0.0; int i; 对于(i=0;i,java,matrix,null,equals,Java,Matrix,Null,Equals,这就是问题所在: import java.util.*; public class Algorithm { public static class Matrix{ private Double[][] x; } public static Scanner scan = new Scanner(System.in); private static String str; public static void read_data(Double[] radians, Dou
import java.util.*;
public class Algorithm {
public static class Matrix{
private Double[][] x;
}
public static Scanner scan = new Scanner(System.in);
private static String str;
public static void read_data(Double[] radians, 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
if (i < 9) str = str.substring(0, str.length()-1); //traiem la coma
radians[i] = Math.toRadians(Double.parseDouble(str));
}
radians[i] = 0.0;
}
public static void print_Matrix(Double[][] M) {
for (int k = 0; k < 4; k++) {
System.out.print("\n");
for (int j = 0; j < 4; j++) {
System.out.print(String.valueOf(M[k][j]) + " , ");
}
}
}
public static void print_Jacobian(Double[][] M) {
System.out.print("Jacobian Matrix =");
for (int k = 0; k < 3; k++) {
System.out.print("\n");
for (int j = 0; j < 9; j++) {
System.out.print(String.valueOf(M[k][j]) + " , ");
}
}
}
public static void init_Matrix(Double[][] M, int i, Double[] radians, Double[] l) {
M[0][3] = l[i];
M[0][0] = Math.cos(radians[i]);
M[0][1] = -Math.sin(radians[i]);
M[1][0] = Math.sin(radians[i]);
M[1][1] = Math.cos(radians[i]);
for (int k = 0; k < 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;
//System.out.print(String.valueOf(M[k][j]) + "\n");
}
}
}
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[] radians) {
M[0][0] = -Math.sin(radians[i]);
M[0][1] = -Math.cos(radians[i]);
M[1][0] = Math.cos(radians[i]);
M[1][1] = -Math.sin(radians[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[] radians, Double[] l) {
int i;
for (i = 0; i < 9; i++) {
Ts[i].x = new Double[4][4];
init_Matrix(Ts[i].x, i, radians, l);
//print_Matrix(Ts[i].x);
}
init_Ultima_Matrix(Ts[i].x, i, l);
}
public static void fulfill_Ds(Matrix[] Ds, Double[] radians) {
int i;
for (i = 0; i < 9; i++) {
Ds[i].x = new Double[4][4];
init_Derivada(Ds[i].x, i, radians);
}
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_T(Matrix[] Ts, Double[][] T) {
T = Ts[0].x;
for (int j = 1; j < 10; j++) {
T = product(T, Ts[j].x);
}
}
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] = new Matrix();
Ts[i].x = new Double[4][4];
Ds[i] = new Matrix();
Ds[i].x = new Double[4][4];
}
Double[] radians = new Double[10];
Double[] l = new Double[10];
read_data(radians, l);
fulfill_Ts(Ts, radians, l);
fulfill_Ds(Ds, radians);
Matrix T = new Matrix();
T.x = new Double[4][4];
System.out.print("\n Ts Matrix =");
for (int q=0; q<10; ++q) print_Matrix(Ts[q].x);
calc_T(Ts, T.x);
System.out.print("\n T Matrix =");
print_Matrix(T.x);
Matrix jacobian = new Matrix();
jacobian.x = new Double[3][9];
for (int j=0; j<9; j++)
calc_Jacobian(Ts, Ds, j, jacobian.x);
print_Jacobian(jacobian.x);
//La matriu Jacobiana hauria d'estar acabada
}
}
Java中的参数是按值传递的。因此,基本上,您会忽略T
的原始值,为其分配一个新值,然后对其不做任何处理-这不会改变调用者的任何事情。您想要的是:
private static void calc_T(Matrix[] Ts, Double[][] T) {
T = Ts[0].x;
...
}
您还应该强烈考虑使用<代码>双倍[]/COD>而不是<代码>双倍[][]除非您需要能够表示空值,并开始遵循Java命名约定。
以后,请将您的代码简化为一个最小的问题示例。您已经发布了近200行缩进严重的代码,而实际上只有一小部分是相关的。我考虑过这样做,但我认为参数是通过的地址,因此我可以更改T的值并使函数无效。这有可能吗?我知道在C中,我可以写入&T以传递地址而不是整个对象。其次,我需要检查空值,因此我必须将形式double[]]更改为double[][].最后,是的,我应该检查一下命名约定,因为我已经忘记了。谢谢Jon@user2211939:不,没有办法将Java更改为使用按引用传递…而且基本上您不想这样做。您正在尝试计算一些private static Double[][] calc_T(Matrix[] Ts) {
Double[] t = Ts[0].x;
for (int j = 1; j < 10; j++) {
t = product(t, Ts[j].x);
}
return t;
}
T.x = calc_T(Ts);