Reputation: 265
I'm trying to drop a student from course, and this is what i got so far..
public void dropStudent() {
String id;
System.out.println(" Enter student ID " );
id = Keyboard.readString();
for ( int i = 0; i <= students.length - 1; i++)
{
if (id.equals(students[i].displayId()))
{
for (int j = i; j <= students.length - 1; j++)
{
students[i] = students[i+1];
}
}
}
}
So the first loop is for each element of the array, the if statement is to check if the id entered match's a student's id from the array. the second "For" is to shift the elements back. when i try it, it does delete a specific student. but instead of shifting the elements back, it just copies the next element in both places. i tried to set the element to null, but it didn't work.
so any suggestions please ? i'm also not allowed to use array list or other things.
Upvotes: 1
Views: 16692
Reputation: 148
I a bit worried how all the responses to this question try to "solve" this problem, but no one mention that this is a bad solution and there are better way to solve this problem.
Try the accepted response to this question.
Upvotes: 0
Reputation: 51443
You can do one of the following:
If you want to eliminate an element in a given 'x' position of the array, you just make a shift left of all the elements on the next position of the array. Keep a size variable and decrease it;
You can have a flag (on the class that have the student information) that will be used to mark if the object is deleted or not. False -> deleted, True -> not deleted.
You can use an ArrayList instead of an array.
If the order does not matter you can simply do:
for ( int i = 0; i <= students.length - 1; i++)
if (id.equals(students[i].displayId()))
students[i] = students[students.length-1];
Just take the last element and put it in the actual gap. However, you have to do something with the last position, someway of mark it as empty (you can set to null for example).
If the order does matter, then you can use an auxiliary array:
Students [] removeFrom(Students [] old_array, int pos){
Students [] new_array = new Students[old_array-1];
for (int j = 0; j < i ; j++) new_array[j] = old_array[j];
for (int j = i; j < new_array.length; j++) new_array[j] = old_array[j+1];
return new_array;
}
in :
for ( int i = 0; i <= students.length - 1; i++)
if (id.equals(students[i].displayId()))
students = removeFrom(students, i);
Upvotes: 2
Reputation: 170
You cannot alter the size of a Java array after instantiation. If all that's needed is returning an array without the student id. You'd want to create a new array with one fewer element, and iterate over the original array, copying all but the id you want to remove.
Normally, you'd want to use lists for this. But given your constraints, that's how I'd tackle it.
Something like:
public String[] dropStudent(String delete, String[] students){
int length=students.length-1, i=0;
String[] remainingStudents= new String[length];
for(String student: students){
// catch the edge case where 'delete' is not found, and avoid the ArrayIndexOutOfBoundsException
if(i==remainingStudents.length){
return students;
}
// Add the any non-matching students to the final array.
if(!student.equals(delete)){
remainingStudents[i]=student;
i++;
}
}
return remainingStudents;
}
Upvotes: 4
Reputation: 395
Upvotes: 2
Reputation: 15523
Replace:
students[i] = students[i+1];
with:
students[j] = students[j+1];
Indeed, you want to go through all the rest of the array with your loop variable j
and not with i
which represents the index of the student to be deleted.
Also, you need to replace your loop:
for (int j = i; j <= students.length - 1; j++)
with
for (int j = i; j <= students.length - 2; j++)
(notice the 1 changed in a 2)
The reason for this is that you are accessing students
with the index j+1, which, if j == students.length - 1, will be equal to students.length, and therefore you will have an ArrayOutOfBounException.
At last, you have to set the last element of the array as null (or the last student will be duplicated). Or better yet, you can resize the array (that is create a new array and copy the new list of students in it).
Also, if you have several copies of the students in the array and you just want to remove the first, you have to add break
when you have found the element to be removed.
Upvotes: 2