2017-10-16 63 views
0

首次發佈時,對我正在處理的表類有疑問。出於某種原因,添加新行不會添加正確的行數。我添加了代碼Java表類結構

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()); 
    } 
} 
+0

請分享你的'Table'類 – Joshua

+0

或者在完整代碼至少[mcve]。 – shmosel

+0

您是否調試過解決方案? –

回答

0

如果你使用一個調試器,你會看到你在ADDCOL添加額外的行

+0

謝謝,我明白了,儘管如此,如果沒有行,它會添加一行。應該可以將列添加到沒有行的表中嗎? – tbarbs

+0

@tbarbs「應該有可能......」這是一個設計問題。這取決於規格是什麼。否則,我相信我已經回答了你的問題......你能檢查這個答案是否正確? – ControlAltDel

+0

好點,可以說是的,你需要有一個行,它被視爲一張桌子。除了如何添加新列以外,還有其他方法嗎?我現在正在添加該行,以便添加一列可以添加一行,如果行數爲0。 – tbarbs