线程" main"中的例外情况java.lang.IndexOutOfBoundsException:Index:2,Size:1

时间:2015-04-13 08:40:20

标签: java arraylist

我执行此代码时获得此异常?请帮忙吗?

public static void main(String[] args) throws SQLException {

    professeur f = new professeur();
    ArrayList<Integer> arl =new ArrayList<Integer>();

    int  k=0;
    etudiant e = new etudiant();
    List<etudiant> list = e.getAll();
    List<professeur> l = f.getAll1();

    for (int i = 0; i < list.size(); i++) {

        for (int j = 0; j < l.size(); j++) {
            if (list.get(j).getIde()==l.get(i).getIdp())  {
                 k=list.get(i).getIde();

                System.out.println(list.get(i).getNome());
            } 
           break;


        }

           professeur p=new professeur();
           List <professeur> c= p.findAllbyID(k);
           System.out.println(c.get(i).getNomp());}



    }

}

1 个答案:

答案 0 :(得分:3)

看起来你的指数已经切换了。

由于i遍历listj的索引会迭代l的索引,因此应该是:

for (int i = 0; i < list.size(); i++) {

    for (int j = 0; j < l.size(); j++) {
        if (list.get(i).getIde()==l.get(j).getIdp())  { // i and j were switched
                                                        // on this line
             k=list.get(i).getIde();

            System.out.println(list.get(i).getNome());
        } 
       break;


    }