数组异常值未插入

Array exception Values not inserting

我有两个 JTable。例如 TB1 和 TB2,每个只有一个列。

每个 table 包含一些值。或者第二个 table 可能是空的。 我只需要

  1. 将 TB1 的值添加到 TB2。

  2. 还需要比较两个 table 值并将其写入 TB2,无需复制

    我正在使用 netbeans-8。

    String[] Ary = {};
    Vector R_D=new Vector();
    int found=0;
    for(int i=0;i<=t1.getRowCount()-1;i++){
        System.out.println(t1.getRowCount());
    
            for(int j=0;j<=t2.getRowCount()-1;j++){
    
                    if(t1.getValueAt(i, 0)!=t2.getValueAt(j, 0)){
                        System.err.println("Compare "+t1.getValueAt(i,     0)+"and "+t2.getValueAt(j, 0));
    
                        found=0;
                    }
                    else{
                        found=1;
                        System.err.println("Found Match at +t1.getValueAt(i,  0)+"and "+t2.getValueAt(j, 0));
                    }
    
            }
            if(found==0){
                        Ary[i]=t1.getValueAt(i, 0).toString();                    
                        R_D.add(t1.getValueAt(i, 0).toString());
            }
                ((DefaultTableModel) t2.getModel()).insertRow(i, R_D);
    }
    

    在这部分 Ary[i]=t1.getValueAt(i, 0).toString();
    我收到一些错误消息,例如:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0

如何避免这种异常?

String[] Ary = {};

您正在创建一个空数组。

也许你想要这样的东西:

String[] array = new String[t1.getRowCount()];

现在你有一个数组,其大小与 table 中的行数相同。