【问题标题】:Adding a row to a pre-existing 2D ArrayList [duplicate]将行添加到预先存在的 2D ArrayList [重复]
【发布时间】:2017-02-28 04:17:37
【问题描述】:

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

我想做的是向一个已经有一些元素的二维数组列表添加一行。由于某些奇怪的原因,我在添加时得到 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");
    }
  }
}

程序输出:

Good
Done Adding Rows (9)
Done Adding Cols (6)
Done Setting
1111 null null null null null 
null 2222 null null null null 
null null 3333 null null null 
null null null 4444 null null 
null null null null 5555 null 
null null null null null 6666 
7777 null null null null null 
null 8888 null null null null 
null null 9999 null null null 

Added Col (7)
Added Row (10)
1111 null null null null null null 
null 2222 null null null null null 
null null null 3333 null null null 
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at comp2402a2.Table.get(Table.java:24)
    at comp2402a2.Table.main(Table.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

【问题讨论】:

    标签: java arraylist multidimensional-array indexoutofboundsexception


    【解决方案1】:

    问题在于您的 addRow 方法。在该方法中,您为要插入的行添加new ArrayList&lt;T&gt;(),稍后当您使用table.get(3).get(0) 访问时,它将抛出IndexOutofBoundsException,因为new ArrayList&lt;T&gt;() 实际上并没有添加任何元素。需要修改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);
      }
    

    【讨论】:

    • 啊哈!是的,我试着做这两件事,但我从来没有把两者放在一起。解释了很多,谢谢!
    猜你喜欢
    • 2017-01-11
    • 2014-03-18
    • 2014-10-14
    • 1970-01-01
    • 2021-04-02
    • 2018-08-19
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多