I have two search loops for performing different operations, but I'm unhappy with how repetitive this looks.
The first method used to remove an item is as follows:
public void RemovePlayer(int theID){
boolean matchFound = false;
if (playerObjects.size() != 0){
for (int i = 0; i < playerObjects.size(); i++){
Person playerToRemove = (Person) playerObjects.get(i);
if (playerToRemove.getID() == theID){
playerObjects.remove(i);
System.out.println("Player with ID # " + theID + " removed");
matchFound = true;
// As ID is unique, once a player is found it is unnecessary to continue looping
break;
}
// If matchFound is never set to true then show appropriate error
if (matchFound == false) {
System.out.println("Player with ID # " + theID + " not found");
}
}
}
else {
System.out.println("No players have been added.");
}
}
And the second method, which is essentially the same code, but performs a different action if a match is found is as follows:
public void RetrievePlayer(int theID){
boolean matchFound = false;
if (playerObjects.size() != 0){
for (int i = 0; i < playerObjects.size(); i++){
Person playerToRetrieve = (Person) playerObjects.get(i);
if (playerToRetrieve.getID() == theID){
System.out.println("PLAYER FOUND:");
playerToRetrieve.GetDetails();
matchFound = true;
break;
}
// If matchFound is never set to true then show appropriate error
if (matchFound == false) {
System.out.println("Player with ID # " + theID + " not found");
}
}
} else {
System.out.println("No players have been added.");
}
}
How can I refactor this?
How about a method "FindPlayer" that returns the index i
of the player? RemovePlayer and RetrievePlayer then just would be:
public void RemovePlayer(int theID){
int playerId = FindPlayer(theID);
if (playerId >= 0) {
playerObjects.remove(playerId);
}
}
public void RetrievePlayer(int theID){
int playerId = FindPlayer(theID);
if (playerId >= 0) {
Person player = (Person) playerObjects.get(playerId);
player.getDetails();
}
}
That "FindPlayer" method would be somewhat like this:
protected int FindPlayer(int theID){
if (playerObjects.size() != 0){
for (int i = 0; i < playerObjects.size(); i++){
Person player = (Person) playerObjects.get(i);
if (player.getID() == theID){
return i;
}
}
System.out.println("Player with ID # " + theID + " not found");
} else {
System.out.println("No players have been added.");
}
return -1;
}
Put the Players in a Map<Integer,Player>
. Then use Map's basic methods ( put
, remove
) instead of looping through a list.
If you split then up, you could have a find
method returning a Player
and a remove
method that took the player as an argument. I'd prefer this over returning an index, since an index may be temporary (eg if someone else added to the list the index might become invalidated).
There's more you can do (perhaps a findAll
or a filter
method with a predicate), but I wouldn't look at these until you have a reason to do so (You Ain't Going to Need it)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.