-1

나는 둘러 보았고 특별히 이슈를 대상으로하는 정확한 기사를 찾을 수 없었습니다. 정보 출처가 있다면 알려주십시오!기존 2D ArrayList에 행 추가하기

내가하고 싶은 것은 이미 2D 요소가있는 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"); 
    } 
    } 
} 

프로그램 출력 :

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) 

답변

2

문제는 addRow 방법이다. 이 방법에서는 삽입하려는 행에 new ArrayList<T>()을 추가하고 나중에 table.get(3).get(0)을 사용하여 액세스 할 때 IndexOutofBoundsException을 던질 것입니다. new ArrayList<T>()은 실제로 요소를 추가하지 않기 때문입니다. 당신은 cols()

는 또한 cols()0 행 크기를 처리해야하고, 그것을 필요도 약간 변형 될 기반으로 요소를 추가하는 방법을 addRow()을 수정해야합니다

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

아하! 네, 저는 그 두 가지를 모두 시도했지만 두 가지를 절대로 같이 쓰지 않았습니다. 고마워, 고마워! – Sythe

+0

나를 이길 ... –

관련 문제