Java 将行添加到预先存在的二维ArrayList

Java 将行添加到预先存在的二维ArrayList,java,arraylist,multidimensional-array,indexoutofboundsexception,Java,Arraylist,Multidimensional Array,Indexoutofboundsexception,我环顾四周,找不到专门针对我的问题的确切文章,所以如果你有信息来源,请告诉我 我想做的是在2D arraylist中添加一行,其中已经有一些元素。出于某种奇怪的原因,我在添加时将IndexOutOfBoundsException:Index:0,Size:0作为错误 我想我理解为什么,因为我刚才添加的行是空的,因此无法访问。但是,即使我将元素列添加到行中,它仍然有相同的错误 public class Table<T>{ List<List<T>> tabl

我环顾四周,找不到专门针对我的问题的确切文章,所以如果你有信息来源,请告诉我

我想做的是在2D arraylist中添加一行,其中已经有一些元素。出于某种奇怪的原因,我在添加时将IndexOutOfBoundsException:Index:0,Size:0作为错误

我想我理解为什么,因为我刚才添加的行是空的,因此无法访问。但是,即使我将元素列添加到行中,它仍然有相同的错误

public class Table<T>{
  List<List<T>> table;

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

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

  public int cols() {
    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();
    T data = table.get(i).get(j);
    table.get(i).set(j, x);
    return data;
  }

  public void addRow(int i) {
    if (i < 0 || i > rows()) throw new IndexOutOfBoundsException();
    table.add(i, new ArrayList<T>());
  }

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

  public void addCol(int j) {
    if (j < 0 || j > cols()) throw new IndexOutOfBoundsException();
    if (rows() == 0) return;
    for(int i = 0; i < rows(); i++){
      table.get(i).add(j, null);
    }
  }

  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) {
    int nrows = 9, ncols = 6;
    Table<Integer> t = new Table<Integer>(Integer.class);
    System.out.println("Good");
    for (int i = 0; i < nrows; i++) {
      t.addRow(t.rows());
    }
    System.out.println("Done Adding Rows ("+t.rows()+")");
    for (int i = 0; i < ncols; i++) {
      t.addCol(t.cols());
    }
    System.out.println("Done Adding Cols ("+t.cols()+")");
    for (int i = 1; i <= nrows; i++) {
      t.set(i-1, (i-1)%t.cols(), 1111*i);
    }
    System.out.println("Done Setting");
    for (int i = 0; i < t.rows(); i++) {
      for (int j = 0; j < t.cols(); j++) {
        System.out.print(t.get(i,j)+ " ");
      }
      System.out.print("\n");
    }
    t.addCol(2);
    System.out.println("Added Col ("+t.cols()+")");
    t.addRow(3);
    System.out.println("Added Row ("+t.rows()+")");
    for (int i = 0; i < t.rows(); i++) {
      for (int j = 0; j < t.cols(); j++) {
        System.out.print(t.get(i,j)+ " ");
      }
      System.out.print("\n");
    }
  }
}
问题在于addRow方法。在该方法中,您正在为要插入的行添加新的ArrayList,稍后使用table.get3.get0进行访问时,它将抛出IndexOutofBoundsException,因为新的ArrayList实际上没有添加任何元素。您需要修改addRow方法以添加基于cols的元素

cols还需要处理0行大小,并且还需要修改:

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



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

    List<T> list = new ArrayList<T>();
    for ( int j = 0; j < cols(); j++ )
    {
        list.add( null );
    }
    table.add(i, list);
  }

啊哈!是的,我试着做这两件事,但我从来没有把这两件事放在一起。解释了很多,谢谢!
public int cols() {
      if ( table.size() > 0 )
    return table.get(0).size();
      else
          return 0;
  }



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

    List<T> list = new ArrayList<T>();
    for ( int j = 0; j < cols(); j++ )
    {
        list.add( null );
    }
    table.add(i, list);
  }