java - Delete object in Arraylist -



java - Delete object in Arraylist -

my problem here when have 2 objects in array loop 2x asks "are sure want delete it?". can't figure out loop. here code:

for (iterator<student> = student.iterator(); it.hasnext();) { pupil stud = it.next(); { system.out.print("are sure want delete it?"); string confirmdelete = scan.next(); ynonly = false; if (confirmdelete.equalsignorecase("y") && stud.getstudnum().equals(enterstudnum2)) { it.remove(); system.out.print("delete successful"); ynonly = false; } else if (confirmdelete.equalsignorecase("n")) { system.out.print("deletion did not proceed"); ynonly = false; } else { system.out.println("\ny or n only\n"); ynonly = true; } } while (ynonly == true); }

its because of 2 loops going on there. inner loops terminate after value of ynonly becomes false outer loop still continues. might want that--

for (iterator<student> = student.iterator(); it.hasnext();) { pupil stud = it.next(); if(!stud.getstudnum().equals(enterstudnum2)) continue; //you want pupil deleted has enterstudnum2 allow other record skip { system.out.print("are sure want delete it?"); string confirmdelete = scan.next(); ynonly = false; if (confirmdelete.equalsignorecase("y") && stud.getstudnum().equals(enterstudnum2)) { it.remove(); system.out.print("delete successful"); ynonly = false; } else if (confirmdelete.equalsignorecase("n")) { system.out.print("deletion did not proceed"); ynonly = false; } else { system.out.println("\ny or n only\n"); ynonly = true; } } while (ynonly == true);

}

java arraylist iterator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -