繁体   English   中英

线程“ main”中的异常java.lang.IndexOutOfBoundsException:索引:2,大小:1

[英]Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 2, Size: 1

我在执行此代码时获得了此异常?

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());}



    }

}

看来您的索引已切换。

由于i遍历list的索引和j遍历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;


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM