java如何以矩阵格式存储ArrayList,ArrayList应该是字符串数据?
在这段代码中,我在ml.getV()
方法中得到了错误ArrayoutofBoundException
和NullPointerException
这是我的矩阵课
public class Matrix {
private double [][] V = null;
// public members
public static int RowsOfVMatrix = 9219;
public static int ColsOfVMatrix = 5;
public static int ROW_INCREASE = 20;
public double[][] getV() {
return V;
}
public void setV(double[][] v) {
V = v;
}
public static int getRowsOfVMatrix() {
return RowsOfVMatrix;
}
public static void setRowsOfVMatrix(int rowsOfVMatrix) {
RowsOfVMatrix = rowsOfVMatrix;
}
public static int getColsOfVMatrix() {
return ColsOfVMatrix;
}
public static void setColsOfVMatrix(int colsOfVMatrix) {
ColsOfVMatrix = colsOfVMatrix;
}
public static int getROW_INCREASE() {
return ROW_INCREASE;
}
public static void setROW_INCREASE(int rOW_INCREASE) {
ROW_INCREASE = rOW_INCREASE;
}
public Matrix(){
V = new double[Matrix.RowsOfVMatrix][Matrix.ColsOfVMatrix];
}
public Matrix(int rowsSize, int colSize){
Matrix.RowsOfVMatrix = rowsSize;
Matrix.ColsOfVMatrix = colSize;
V = new double[Matrix.RowsOfVMatrix][Matrix.ColsOfVMatrix];
}
public Matrix(Matrix m){
V = new double[Matrix.RowsOfVMatrix][Matrix.ColsOfVMatrix];
for (int i = 0; i < Matrix.RowsOfVMatrix; i++) {
for (int j = 0; j < Matrix.ColsOfVMatrix; j++) {
this.V[i][j] = m.V[i][j];
}
}
}
我使用以下代码在Matrix
中尝试了这个代码存储ArrayList
public static List<Matrix> readFromFileGetMatrixList(String filename)
throws IOException {
List<Matrix> ml = new ArrayList<Matrix>();
Matrix m = null;
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String lineContent;
int matrixNum = 0;
while ((lineContent = br.readLine()) != null) {
if (lineContent.contains("#")) {
matrixNum++;
}
}
matrixNum--;// the last one may be incomplete,so remove it from list
br.close();
fr.close();
fr = new FileReader(filename);
br = new BufferedReader(fr);
int matrixRow_index = 0;
int matrixCount = 0;
while ((lineContent = br.readLine()) != null) {
if (lineContent.contains("#")) {
if (matrixCount > 0) {
ml.add(m);
matrixRow_index = 0;
}
if (matrixCount == matrixNum)
break;
m = new Matrix();
continue;
}
if (lineContent.equals("")) {
matrixCount++;
continue;
}
String[] str = lineContent.split("\t");
for (int j = 0; j < str.length; j++) {
m.getV()[matrixRow_index][j] = Double.parseDouble(str[j]); //Null pointer Exception
}
matrixRow_index++;
}
return ml;
}
这是我的文本文件示例
0 qid:1 1:1.000000 2:0.693147 3:0.166667 #docid = 285257 0 qid:1 1:1.000000 2:0.693147 3:0.166667 #docid = 285285 0 qid:1 1:1.000000 2:0.693147 3:0.166667 #docid = 285289 0 qid:1 1:1.000000 2:0.693147 3:0.166667 #docid = 285245
在上面的代码中,我得到了ArrayBoundofIndexError
共 (0) 个答案