Wednesday, 15 February 2012

java - How to delete an element from an array? -



java - How to delete an element from an array? -

i'm trying drop pupil course, , got far..

public void dropstudent() { string id; system.out.println(" come in pupil id " ); id = keyboard.readstring(); ( int = 0; <= students.length - 1; i++) { if (id.equals(students[i].displayid())) { (int j = i; j <= students.length - 1; j++) { students[i] = students[i+1]; } } } }

so first loop each element of array, if statement check if id entered match's student's id array. sec "for" shift elements back. when seek it, delete specific student. instead of shifting elements back, copies next element in both places. tried set element null, didn't work.

so suggestions please ? i'm not allowed utilize array list or other things.

use variable "j" in sec loop fix logic "j+1" not result array index out of bounds

java

No comments:

Post a Comment