Return a value present in loop in java -
i want return value loop. code follows:
public long findlocationid(string location) throws systemexception { long locid = 1; list<location> locationlist = locationlocalserviceutil.getlocations(-1,-1); for(location findloc : locationlist) { if(location == findloc.getlocationname()) { locid = findloc.getlocationid(); **return locid;** } } }
when try put return value in loop error saying include return statement.
how should change code can return value loop itself?
i want return new locid value in loop , not value set locid = 1; want return new value of locid loop
there various approach problem.
- use while loop
- use loop additional stop condition
- use break key word.
let first create template before introduce our logic:
public long findlocationid(string locationname) throws systemexception { if(locationname == null) { //here cover first issue. throw new illegalargumentexception("the locationname must not null"); } long locid = long.min_value; //we declare default value returned if none match found. collection<location> locationlist = getlocationlist(); //the location can read method not binded field. if(locationlist == null || locationlist.isempty()) { return locid; // or throw exception invalid state. } //place logic return locid; }
typically when not know when want stop iteration, sign should start while loop.
so lets try it.
solution 1 - while way.
iterator<location> iterator = locationlist.iterator(); while(iterator.hasnext() && long.min_value != locid) { location location = iterator.next(); if(locationname.equalsignorecase(location.getlocationname())) { locid = location.getlocationid(); // change locid, second condition no longer true , loop end. } }
the pros: - works
the cons: - leave iterator
we should not leave iterators, error prone. lead next solution.
solution 2 - use pattern iterator instead of while.
for(iterator<location> iterator2 = locationlist.iterator();iterator.hasnext() && long.min_value != locid;) { location location = iterator.next(); if(locationname.equalsignorecase(location.getlocationname())) { locid = location.getlocationid(); // change locid, second condition no longer true , loop end. } }
pros - works
cons - complicated, must thing stop, when reading code.
as above solution not easy read should removed.
solution 3 - why break useful.
for(location location : locationlist) { if(locationname.equalsignorecase(location.getlocationname())) { locid = location.getlocationid(); break; } }
pros - works - readable
cons - none
conclusion code should readable. using break
, point found match , not want progress anymore.
fine. case when location
found ?
op example return 1l
. not best choice value used id.
in previous examples have used min value of long. acceptable cases but, still need validate method result, , document it.
the final solution present additional loop exit, return
key word.
public long findlocationid(string locationname) throws systemexception { if(locationname == null) { //here cover fist issue. throw new illegalargumentexception("the locationname must not null"); } collection<location> locationlist = getlocationlist(); //the location can read method not binded field. if(locationlist == null) { throw new illegalstateexception("the location list not initialized"); } for(location location : locationlist) { if(locationname.equalsignorecase(location.getlocationname())) { return location.getlocationid(); //we exit method. } } throw new systemexception("could not found location name:" + locationname); }
additional note
in example op have location == findloc.getlocationname()
, problem code should not use ==
compare objects types (details). deal string class recommended method use method string#equals(object)
or 'string#equalsignorecase(string)'. example have used second option.
Comments
Post a Comment