5

I want to make a look up for entry in ArrayList and remove element if it's found, the simplest way I guess is through Iterator, here's my code:

    for (Iterator<Student> it = school.iterator(); it.hasNext();){
        if (it.equals(studentToCompare)){
            it.remove();
            return true;
        }
        System.out.println(it.toString());
        it.next();
    }

But something is wrong: instead of iterating through my ArrayList<Student> school I get using it.toString():

java.util.ArrayList$Itr@188e490
java.util.ArrayList$Itr@188e490
...

What's wrong?

3
  • List has .remove()!! Commented Jun 15, 2013 at 11:55
  • You are doing a toString() of the iterator, not of Student Commented Jun 15, 2013 at 11:56
  • ArrayList$Itr@188e490 is the address of the iterator itself. Commented Jun 15, 2013 at 11:57

4 Answers 4

22

it is a Iterator, not Student

for (Iterator<Student> it = school.iterator(); it.hasNext();){
    Student student = it.next();
    if (student.equals(studentToCompare)){
        it.remove();
        return true;
    }
    System.out.println(student.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

14

Why not ?

school.remove(studentToCompare);

Use List remove(Object) method.

Removes the first occurrence of the specified element from this list, if it is present (optional operation).

And moreover

It Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

2 Comments

Could highlight the fact that it both returns true on success, AND that it removes the first element; both having the cake and eating it given OP's question
Your answer is the preferred one. But the iterator will be useful when you are looping through the list and based on some condition you would like to remove an element from the list being inside the loop.
1
for (Iterator<Student> it = school.iterator(); it.hasNext();){
    **Student st** = it.next();
    if (**st**.equals(studentToCompare)){
        it.remove();
        return true;
    }
    System.out.println(**st**.toString());
}

OR

school.remove(school.indexOf(studentToCompare));

OR

school.remove(studentToCompare);

The latter two examples assume school is a List.

Comments

1

The reason you are getting this output is because you are calling toString() method on iterator and not on the Student object.

You can remove student from the list by using

school.remove(student);

Also, if you want to print meaningful Student object information when you write

System.out.println(student);

Override toString() method for it as System.out.println() statement would internally call toString() on student object.

public String toString() {
    return "Student [id=" + id + ", firstName=" + firstName
            + ", lastName=" + lastName + "]";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.