【发布时间】: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