I'm trying to remove a given object from the array list.
But it doesn't work.
what's the problem?
Code:public boolean removeSomething(Student student)
{
student.remove(student);
return true;
}
Printable View
I'm trying to remove a given object from the array list.
But it doesn't work.
what's the problem?
Code:public boolean removeSomething(Student student)
{
student.remove(student);
return true;
}
I would guess that the student you are trying to remove is not found in the list so it is not reomving anything.
The reasons for this could be:
1) It isn't there
2) The equals method for the student in the list and the one you are passing in do not match.
Are you creating the student manually or retrieving it from the list prior to removing it?
If that's not the case then I think you need to provide a little more information then we can give you a bit more help
Regards :D
This is my code
Code:public boolean remove(Student student)
{
if(student==null)
{
return false;
}
else
{
students.remove(student);
return true;
}
}
I was more after the code around what you have here.
How are you obtaining the student instance which you are passing in to this method?
Do you have a get method on this class which does :
somewhere in it? like:Code:students.get(n);
If this is the case then passing the returned student into the remove function should work, however if you have created a new instance of Student then this may not work.Code:public Student findWithName(String name) {
Student student = null;
Iterator<Student> iterator = students.iterator();
while(null == student && iterator.hasNext()) {
Student potential = iterator.next();
if(potential.getName.equalsIgnoreCase(name)) {
student = potential;
}
}
return student;
}
Can you show me the code which calls remove and also the code for Student?
Thanks.