这是我第一次使用java,我在使代码工作时遇到了一些困难。我有两个.java文件,主要文件和测试文件。
这是主要代码:
public class Matrix {
double[][] array;
double[][] matrix;
public Matrix(double[][] array) {
double[][] matrix = new double[array.length][];
for(int i=0; i<array.length; i++)
matrix[i] = array[i].clone();
}
public double[] getRow(int rownum) {
int x;
for(x=0; x<matrix[rownum].length; x++){
System.out.print(matrix[rownum][x]);
}
System.out.println();
return matrix[rownum];
}
public void swap(int rowA, int rowB) {
double temp[] = matrix[rowA];
matrix[rowA] = matrix[rowB];
matrix[rowB] = temp;
}
public int maxRow(int column_num) {
int y;
int maxLoc;
double maxVal;
Line 29 maxVal = matrix[0][column_num];
maxLoc = 0;
for(y=0; y<matrix.length; y++){
if(maxVal<matrix[y][column_num]){
maxVal = matrix[y][column_num];
maxLoc = y;
}
}
return maxLoc;
}
public void scaleRow(int rownum, double factor) {
// multiply all elements of current matrix in row rownum by factor
int z;
for(z=0; z<matrix[rownum].length; z++){
matrix[rownum][z] = matrix[rownum][z]*factor;
}
}
}
测试文件代码是这样的:
import java.util.Arrays;
public class MatrixTest {
public static void main(String[] args) {
/* test cases for Matrix class methods */
double[][] matrixA = {{1,2,3,4},{2,4,6,8},{9,10,11,12}};
double[][] matrixB = {{0.92,-900,44},{1201,18.264,-21.0},{0,0,0}};
double[][] matrixC = {{1.5E-12,-6.034E2},{41.8,-125E-3}};
Matrix One = new Matrix(matrixA);
Matrix Two = new Matrix(matrixB);
Matrix Three = new Matrix(matrixC);
/* test whether or not maxRow works properly */
Line 12 if (One.maxRow(0) == 2) {
System.out.println("Passed maxRow test for matrix One"); }
else { System.out.println("Failed maxRow test for matrix One"); }
if (Two.maxRow(1) == 1) {
System.out.println("Passed maxRow test for matrix Two"); }
else { System.out.println("Failed maxRow test for matrix Two"); }
/* test whether or not scaleRow works properly */
One.scaleRow(0,2.0); // scale row 0 by 2.0
if (Arrays.equals(One.getRow(0),matrixA[1])) {
System.out.println("Passed scaleRow test for matrix One"); }
else { System.out.println("Failed scaleRow test for matrix One"); }
Two.scaleRow(2,12.608); // scale row 2 by 12.608
if (Arrays.equals(Two.getRow(2),matrixB[2])) {
System.out.println("Passed scaleRow test for matrix Two"); }
else { System.out.println("Failed scaleRow test for matrix Two"); }
One.scaleRow(0,0.5); // scale row 0 by 0.5
if (Arrays.equals(One.getRow(0),matrixA[0])) {
System.out.println("Passed scaleRow test for matrix Three"); }
else { System.out.println("Failed scaleRow test for matrix Three"); }
/* test whether or not swap method works properly */
One.swap(2,0); // swap contents of Row 2 with Row 0
if (Arrays.equals(One.getRow(0),matrixA[2]) &&
Arrays.equals(One.getRow(2),matrixA[0])) {
System.out.println("Passed swap test for matrix One"); }
else { System.out.println("Failed swap test for matrix One"); }
Two.swap(0,1); Two.swap(1,2); Two.swap(1,0); // fancy swap sequence
if (Arrays.equals(Two.getRow(0),matrixB[2]) &&
Arrays.equals(Two.getRow(2),matrixB[0])) {
System.out.println("Passed swap test for matrix Two"); }
else { System.out.println("Failed swap test for matrix Two"); }
}
}
我没有制作测试文件,它是给我们的。我们无法更改测试文件中的任何内容,我们只需要创建自己的Matrix文件。
我可以编译代码,当我运行它时,我得到一个错误说明: “线程中的异常”主“java.lang.NullPointerException。在Matrix.maxRow(Matrix.java:29)和MatrixTest.main(MatrixTest.java:12)”
我试图在声明它时初始化maxVal变量。那时我的代码看起来像:“double maxVal = new double [matrix [0] [column_num]];”但这不起作用,错误说“double []不能转换为double”。我也尝试将其初始化为“double maxVal = new double [-99999999];”但那也不起作用。
就像我之前说的那样,我对java很新,可能还缺少一些非常明显的东西。我很感激您提供的任何帮助!