I have a Class called AuctionItem
. The AuctionItem
Class has a method called getName()
that returns a String
. If I have an ArrayList
of type AuctionItem
, what is the best way to return the index of an item in the ArrayList
that has a specific name?
I know that there is an .indexOf()
function. The parameter for this function is an object. To find the item that has a name, should I just use a for loop, and when the item is found, return the element position in the ArrayList
?
Is there a better way?
I think a for-loop should be a valid solution :
public int getIndexByname(String pName)
{
for(AuctionItem _item : *yourArray*)
{
if(_item.getName().equals(pName))
return *yourarray*.indexOf(_item)
}
return -1;
}