我执行此代码时获得此异常?请帮忙吗?
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());}
}
}
答案 0 :(得分:3)
看起来你的指数已经切换了。
由于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;
}