title: Java Table class structure
body: <p>First time posting, have a question about a table class I'm working on. For some reason adding a new row doesn't add the correct number of rows. I've added the code</p>
  
  <pre><code>public void addRow(int i) {
      if (i &lt; 0 || i &gt; rows()) throw new IndexOutOfBoundsException();
  
      table.add(i, new ArrayList&lt;T&gt;());
  
      for(int j = 0; j &lt; cols(); j++) {
          table.get(i).add(null);
      }
  }
  
  public void addCol(int j) {
      if (j &lt; 0 || j &gt; cols()) throw new IndexOutOfBoundsException();
  
      if(rows() == 0) {
          addRow(0);
      }
  
      for (int i = 0; i &lt; rows(); i++) {
          table.get(i).add(j, null);
      }
  }
  </code></pre>
  
  <p>These here are the methods I have to add a new row and column to the table. Below is what I'm using to test. For some reason it's adding a 5th row. Not sure where from.</p>
  
  <pre><code>Table&lt;Integer&gt; table = new Table&lt;&gt;(Integer.class);
  
  for(int i = 0; i &lt; 4; i++) {
      table.addCol(table.cols());
  }
  
  for(int i = 0; i &lt; 4; i++) {
      table.addRow(table.rows());
  }
  </code></pre>
  
  <p>Any help or ideas would be much appreciated, thanks!</p>
  
  <p>Full Code: </p>
  
  <pre><code>public class Table&lt;T&gt; implements AbstractTable&lt;T&gt; {
  
      List&lt;List&lt;T&gt;&gt; table;
  
      public Table(Class&lt;T&gt; t) { table = new ArrayList&lt;&gt;(); }
  
      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 &lt; 0 || i &gt; rows() - 1 || j &lt; 0 || j &gt; cols()-1)
              throw new IndexOutOfBoundsException();
  
          return table.get(i).get(j);
      }
  
      public T set(int i, int j, T x) {
          if (i &lt; 0 || i &gt; rows() - 1 || j &lt; 0 || j &gt; cols()-1)
              throw new IndexOutOfBoundsException();
  
          return table.get(i).set(j, x);
      }
  
      public void addRow(int i) {
          if (i &lt; 0 || i &gt; rows()) throw new IndexOutOfBoundsException();
  
          table.add(i, new ArrayList&lt;T&gt;());
  
          for(int j = 0; j &lt; cols(); j++) {
              table.get(i).add(null);
          }
      }
  
      public void addCol(int j) {
          if (j &lt; 0 || j &gt; cols()) throw new IndexOutOfBoundsException();
  
          if(rows() == 0) {
              addRow(0);
          }
  
          for (int i = 0; i &lt; rows(); i++) {
              table.get(i).add(j, null);
          }
      }
  
      public void removeRow(int i) {
      if (i &lt; 0 || i &gt; rows() - 1) throw new IndexOutOfBoundsException();
  
      table.remove(i);
      }
  
      public void removeCol(int j) {
          if (j &lt; 0 || j &gt; cols() - 1) throw new IndexOutOfBoundsException();
  
          for (int i = 0; i &lt; rows(); i++) {
              table.get(i).remove(j);
          }
      }
  
      public static void main(String[] args) {
          Table&lt;Integer&gt; table = new Table&lt;&gt;(Integer.class);
          for(int i = 0; i &lt; 4; i++) {
              table.addCol(table.cols());
          }
          for(int i = 0; i &lt; 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());
      }
  }
  </code></pre>