Java中的Basic Matrix,rowCounter不起作用

时间:2017-07-06 13:33:09

标签: java matrix

Hello for Education目的我正在使用Java进行基本的Matrix实现。对于这个矩阵,应该有方法getRowCount()。出于某种原因,我的Matrix没有通过测试,我找不到错误

这是我的方法:

public class MyMatrix<T> implements Matrix<T> {
    Map<Point, T> matrixMap = new HashMap<Point, T>();

    public int getRowCount() {
        double buff;
        ArrayList<Double> rows = new ArrayList<>();
        for (HashMap.Entry<Point, T> entry : matrixMap.entrySet())
        {
            rows.add(entry.getKey().getY());
        }
        Collections.sort(rows);
        buff = rows.get(rows.size()-1);
        int rowCounter = (int) buff;
        rowCounter = rowCounter +1;
        return rowCounter;
    }

public T put(int row, int column, T value) {
    T object = this.get(column, row);
    Point p = new Point();
    p.x = column;
    p.y = row;
    this.matrixMap.put(p, value);
    return object ;
}

这是测试:

public void testGetRowCount() {
        assertEquals("MyMatrix.getRowCount() should return the current number of rows of the matrix!", 3,
                filledMatrix.getRowCount());
        filledMatrix.put(2, 0, "c");
        assertEquals("MyMatrix.getRowCount() should return the current number of rows of the matrix!", 3,
                filledMatrix.getRowCount());
        filledMatrix.put(3, 0, "c");
        assertEquals("MyMatrix.getRowCount() should return the current number of rows of the matrix!", 4,
                filledMatrix.getRowCount());
        filledMatrix.put(6, 0, "c");
        assertEquals("MyMatrix.getRowCount() should return the current number of rows of the matrix!", 7,
                filledMatrix.getRowCount());

        assertEquals("MyMatrix.getRowCount() should return the current number of rows of the matrix!", 0,
                emptyMatrix.getRowCount());
    }

这是我测试的输出:

 MyMatrix<Integer> test = new MyMatrix<Integer>();
        System.out.println(test.put(5,3,5));
        test.put(3,6,9);
        test.put(8,9,9);
        System.out.println(test.getRowCount());

null
9

这是单元测试的输出:

java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:418)
    at java.util.ArrayList.get(ArrayList.java:431)
    at MyMatrix.getRowCount(MyMatrix.java:19)
    at MyMatrixTest.testGetRowCount(MyMatrixTest.java:44)

谢谢你的帮助。

0 个答案:

没有答案