I have a Arraylist of rabbits.
Each rabbit has a String name and a String[] of information.
rabbit(String name, String[] informations)
I have these rabbits in a ArrayList, let’s call it rabbitlist.
I want to make a method like:
public void removerabbit(String name)
I have to make some kind of while loop I guess or a for each loop to get the names out.
I have made an accessor getName() that gives the name of a rabbit.
Just having problems to wrap my brain around to figure out how. Should be simple.
When the name of your
Rabbitis an unique identifier for theRabbit, you can override theequalsmethod ofRabbitto only check for the name. In that case, theremoveRabbitmethod becomes very straightforward since the build-in remove method of theArrayListuses the equals method to determine which element to remove.In case that’s not possible, you can iterate over the List. The following code will remove all Rabbits with the specified name. If you only want to remove the first, you should stop the loop once you removed the first rabbit.