Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:31:34+00:00 2026-06-02T22:31:34+00:00

I have two search loops for performing different operations, but I’m unhappy with how

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:31:35+00:00Added an answer on June 2, 2026 at 10:31 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query to search in two SQL tables. I am looking for
I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first
I have to make an uninformed search (Breadth-first-Search) program which takes two nodes and
I have two different Search activities implemented with SearchManager in my app. One is
I have an array of structs where I perform two searches. First I search
I have two controls on a page, one is a search entry and submission
I have two models in my Django application, for the purposes of storing search
I have two masterpages. A main.Master and a search.Master. The search.Master is a nested
I have a list of two-item lists and need to search for things in
We used to have a search, that checks two columns for some words. Both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.