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.

  1. for case when no female present , putting return null in end of function job, because first return statement won't executed @ all.

  2. for second question, using iterator .. replace for loop

    while(itr.hasnext()) { person newperson=itr.next(); if(newperson.getgender().equals("female") return newperson.getname(); }


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -