i have class called auctionitem. auctionitem class has method called getname() returns string. if have arraylist of type auctionitem, best way return index of item in arraylist has specific name?
i know there .indexof() function. parameter function object. find item has name, should use loop, , when item found, return element position in arraylist?
is there better way?
i think for-loop should valid solution :
public int getindexbyname(string pname) { for(auctionitem _item : *yourarray*) { if(_item.getname().equals(pname)) return *yourarray*.indexof(_item) } return -1; }
Comments
Post a Comment