繁体   English   中英

Java Table类结构

[英]Java Table class structure

第一次发布时,对我正在处理的表类有疑问。 由于某种原因,添加新行不会添加正确的行数。 我已经添加了代码

public void addRow(int i) {
    if (i < 0 || i > rows()) throw new IndexOutOfBoundsException();

    table.add(i, new ArrayList<T>());

    for(int j = 0; j < cols(); j++) {
        table.get(i).add(null);
    }
}

public void addCol(int j) {
    if (j < 0 || j > cols()) throw new IndexOutOfBoundsException();

    if(rows() == 0) {
        addRow(0);
    }

    for (int i = 0; i < rows(); i++) {
        table.get(i).add(j, null);
    }
}

这些是我必须在表中添加新行和新列的方法。 以下是我要测试的内容。 由于某种原因,它添加了第五行。 不知道从哪里来。

Table<Integer> table = new Table<>(Integer.class);

for(int i = 0; i < 4; i++) {
    table.addCol(table.cols());
}

for(int i = 0; i < 4; i++) {
    table.addRow(table.rows());
}

任何帮助或想法将不胜感激,谢谢!

完整代码:

public class Table<T> implements AbstractTable<T> {

    List<List<T>> table;

    public Table(Class<T> t) { table = new ArrayList<>(); }

    public int rows() { return table.size(); }

    public int cols() {
        if(rows() == 0) {
            return 0;
        } else {
            return table.get(0).size();
        }
    }

    public T get(int i, int j) {
        if (i < 0 || i > rows() - 1 || j < 0 || j > cols()-1)
            throw new IndexOutOfBoundsException();

        return table.get(i).get(j);
    }

    public T set(int i, int j, T x) {
        if (i < 0 || i > rows() - 1 || j < 0 || j > cols()-1)
            throw new IndexOutOfBoundsException();

        return table.get(i).set(j, x);
    }

    public void addRow(int i) {
        if (i < 0 || i > rows()) throw new IndexOutOfBoundsException();

        table.add(i, new ArrayList<T>());

        for(int j = 0; j < cols(); j++) {
            table.get(i).add(null);
        }
    }

    public void addCol(int j) {
        if (j < 0 || j > cols()) throw new IndexOutOfBoundsException();

        if(rows() == 0) {
            addRow(0);
        }

        for (int i = 0; i < rows(); i++) {
            table.get(i).add(j, null);
        }
    }

    public void removeRow(int i) {
    if (i < 0 || i > rows() - 1) throw new IndexOutOfBoundsException();

    table.remove(i);
    }

    public void removeCol(int j) {
        if (j < 0 || j > cols() - 1) throw new IndexOutOfBoundsException();

        for (int i = 0; i < rows(); i++) {
            table.get(i).remove(j);
        }
    }

    public static void main(String[] args) {
        Table<Integer> table = new Table<>(Integer.class);
        for(int i = 0; i < 4; i++) {
            table.addCol(table.cols());
        }
        for(int i = 0; i < 4; i++) {
            table.addRow(table.rows());
        }
        System.out.println("rows: " + table.rows() + "\ncols: " + table.cols());

        table.removeCol(1);
        table.removeRow(1);

        System.out.println("rows: " + table.rows() + "\ncols: " + table.cols());
    }
}

如果您使用了调试器,则会看到您正在addCol中添加额外的行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM