java - how to use Iterator in object arraylist -
here got
public class planet { arraylist<person> person = new arraylist<>(); iterator<person> itr = person.iterator(); public string getfemalename() { (int = 0;a < person.size();a++) { if (person.getgender == "female") { return person.get(a).getname(); } } } }
now i'm having 2 problems, 1st want return female's name,but seems have return there no female in arraylist. 2nd how use iterator instead of using loop.
for case when no female present , putting
return null
in end of function job, because firstreturn
statement won't executed @ all.for second question, using iterator .. replace
for
loopwhile(itr.hasnext()) { person newperson=itr.next(); if(newperson.getgender().equals("female") return newperson.getname(); }
Comments
Post a Comment