Re: iterator.
- From: "VisionSet" <spam@xxxxxxxxxxxx>
- Date: Thu, 29 Dec 2005 19:18:20 GMT
"ste1986" <steiddon_1@xxxxxxxxxxx> wrote in message
news:73094ec574c1a58aebddecfff73e2086@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> im curently doing an assignment for uni and there is somthing wrong with
> the code below but i dont know what.
>
> public volunteer.add(new Volunteer(String name, int telephone))
> {
> Iterator it = volunteers.iterator();
> while (it.hasNext())
> {
> Volunteers volunteers = (Volunteers) it.next();
> if (volunteers.getName().equals(name))
> return true;
> }
> return false;
> }
> }
>
'Something wrong' is not helpful. What is wrong? Quote a stack traces or
compiler errors.
However try this to begin with
// create new Volunteer
Volunteer volunteer = new Volunteer(String name, int telephone);
// Call add method with new volunteer
add(volunteer);
// add method
public boolean add(Volunteer volunteer)
{
Iterator it = volunteers.iterator();
while (it.hasNext())
{
Volunteers volunteers = (Volunteers) it.next();
if (volunteers.getName().equals(volunteer.getName())) //
does existing collection contain volunteer?
return false; // false indictating can't add
}
volunteers.add(volunteer); // no volunteer of this name, so add
return true; // return true indicating success
}
}
You'll need a getName() method in your Voluteer class which I imagine you
have or should have.
--
Mike W
.
- Follow-Ups:
- Re: iterator.
- From: Roedy Green
- Re: iterator.
- References:
- iterator.
- From: ste1986
- iterator.
- Prev by Date: Re: iterator.
- Next by Date: Re: Constructor Conundrum
- Previous by thread: Re: iterator.
- Next by thread: Re: iterator.
- Index(es):
Relevant Pages
|