Java - How To Compare Elements In An Object List












-1















I am trying to get and compare average scores of a Player object stored in a List, where every object has a first name, last name, and a list of scores. An example of this would be (List with one object):



[0] Firstname: "Michael", Lastname: "Morris", Scores: [88, 92]



I have code with a nested for loop within a for loop that gives me an OutOfBounds error. I need to get the average of scores per Player and return the highest Player with the highest average. Here's what I have at the moment (The naming is kind off):



public static PlayerScores findHighestScorer(List<PlayerScores> players) {
String team = players.toString();

PlayerScores thePlayers = new PlayerScores("", ""); // What to return
for (int i = 0; i < team.length() - 1; ++i) { // Get every player and their average score
PlayerScores player = players.get(i); // First player
int avg = player.getAverage();
for (int j = i + 1; j < team.length(); ++j) {
PlayerScores player2 = players.get(j); // FIXME: OutOfBoundsException Error
int avg2 = player2.getAverage();
if (avg2 < avg) {
thePlayers = player;
} else if (avg2 > avg) {
thePlayers = player2;
} else {
thePlayers = player;
}
}
}

return thePlayers;


Any and all suggestions would be very helpful, thank you!










share|improve this question




















  • 1





    Why are you checking bounds of teams array in the second loop when you are fetching from players?

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:09











  • because I don't have anything to use as a length other than the team string

    – J. Java
    Nov 18 '18 at 2:13











  • You need to post this data structure because your trouble traversing it is hard to diagnose otherwise. Seems like you could use the length of players list to get a player.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:15













  • I got the code to work after changing "team.length()" to "players.size()," letting me delete the team string

    – J. Java
    Nov 18 '18 at 2:19











  • I strongly suggest you visit oracle documentation on all the accessible properties of an object implementing list interface. Or anything else java related for that matter.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:20
















-1















I am trying to get and compare average scores of a Player object stored in a List, where every object has a first name, last name, and a list of scores. An example of this would be (List with one object):



[0] Firstname: "Michael", Lastname: "Morris", Scores: [88, 92]



I have code with a nested for loop within a for loop that gives me an OutOfBounds error. I need to get the average of scores per Player and return the highest Player with the highest average. Here's what I have at the moment (The naming is kind off):



public static PlayerScores findHighestScorer(List<PlayerScores> players) {
String team = players.toString();

PlayerScores thePlayers = new PlayerScores("", ""); // What to return
for (int i = 0; i < team.length() - 1; ++i) { // Get every player and their average score
PlayerScores player = players.get(i); // First player
int avg = player.getAverage();
for (int j = i + 1; j < team.length(); ++j) {
PlayerScores player2 = players.get(j); // FIXME: OutOfBoundsException Error
int avg2 = player2.getAverage();
if (avg2 < avg) {
thePlayers = player;
} else if (avg2 > avg) {
thePlayers = player2;
} else {
thePlayers = player;
}
}
}

return thePlayers;


Any and all suggestions would be very helpful, thank you!










share|improve this question




















  • 1





    Why are you checking bounds of teams array in the second loop when you are fetching from players?

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:09











  • because I don't have anything to use as a length other than the team string

    – J. Java
    Nov 18 '18 at 2:13











  • You need to post this data structure because your trouble traversing it is hard to diagnose otherwise. Seems like you could use the length of players list to get a player.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:15













  • I got the code to work after changing "team.length()" to "players.size()," letting me delete the team string

    – J. Java
    Nov 18 '18 at 2:19











  • I strongly suggest you visit oracle documentation on all the accessible properties of an object implementing list interface. Or anything else java related for that matter.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:20














-1












-1








-1








I am trying to get and compare average scores of a Player object stored in a List, where every object has a first name, last name, and a list of scores. An example of this would be (List with one object):



[0] Firstname: "Michael", Lastname: "Morris", Scores: [88, 92]



I have code with a nested for loop within a for loop that gives me an OutOfBounds error. I need to get the average of scores per Player and return the highest Player with the highest average. Here's what I have at the moment (The naming is kind off):



public static PlayerScores findHighestScorer(List<PlayerScores> players) {
String team = players.toString();

PlayerScores thePlayers = new PlayerScores("", ""); // What to return
for (int i = 0; i < team.length() - 1; ++i) { // Get every player and their average score
PlayerScores player = players.get(i); // First player
int avg = player.getAverage();
for (int j = i + 1; j < team.length(); ++j) {
PlayerScores player2 = players.get(j); // FIXME: OutOfBoundsException Error
int avg2 = player2.getAverage();
if (avg2 < avg) {
thePlayers = player;
} else if (avg2 > avg) {
thePlayers = player2;
} else {
thePlayers = player;
}
}
}

return thePlayers;


Any and all suggestions would be very helpful, thank you!










share|improve this question
















I am trying to get and compare average scores of a Player object stored in a List, where every object has a first name, last name, and a list of scores. An example of this would be (List with one object):



[0] Firstname: "Michael", Lastname: "Morris", Scores: [88, 92]



I have code with a nested for loop within a for loop that gives me an OutOfBounds error. I need to get the average of scores per Player and return the highest Player with the highest average. Here's what I have at the moment (The naming is kind off):



public static PlayerScores findHighestScorer(List<PlayerScores> players) {
String team = players.toString();

PlayerScores thePlayers = new PlayerScores("", ""); // What to return
for (int i = 0; i < team.length() - 1; ++i) { // Get every player and their average score
PlayerScores player = players.get(i); // First player
int avg = player.getAverage();
for (int j = i + 1; j < team.length(); ++j) {
PlayerScores player2 = players.get(j); // FIXME: OutOfBoundsException Error
int avg2 = player2.getAverage();
if (avg2 < avg) {
thePlayers = player;
} else if (avg2 > avg) {
thePlayers = player2;
} else {
thePlayers = player;
}
}
}

return thePlayers;


Any and all suggestions would be very helpful, thank you!







java object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 2:09









x80486

3,12211442




3,12211442










asked Nov 18 '18 at 2:00









J. JavaJ. Java

31




31








  • 1





    Why are you checking bounds of teams array in the second loop when you are fetching from players?

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:09











  • because I don't have anything to use as a length other than the team string

    – J. Java
    Nov 18 '18 at 2:13











  • You need to post this data structure because your trouble traversing it is hard to diagnose otherwise. Seems like you could use the length of players list to get a player.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:15













  • I got the code to work after changing "team.length()" to "players.size()," letting me delete the team string

    – J. Java
    Nov 18 '18 at 2:19











  • I strongly suggest you visit oracle documentation on all the accessible properties of an object implementing list interface. Or anything else java related for that matter.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:20














  • 1





    Why are you checking bounds of teams array in the second loop when you are fetching from players?

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:09











  • because I don't have anything to use as a length other than the team string

    – J. Java
    Nov 18 '18 at 2:13











  • You need to post this data structure because your trouble traversing it is hard to diagnose otherwise. Seems like you could use the length of players list to get a player.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:15













  • I got the code to work after changing "team.length()" to "players.size()," letting me delete the team string

    – J. Java
    Nov 18 '18 at 2:19











  • I strongly suggest you visit oracle documentation on all the accessible properties of an object implementing list interface. Or anything else java related for that matter.

    – Lucas Kot-Zaniewski
    Nov 18 '18 at 2:20








1




1





Why are you checking bounds of teams array in the second loop when you are fetching from players?

– Lucas Kot-Zaniewski
Nov 18 '18 at 2:09





Why are you checking bounds of teams array in the second loop when you are fetching from players?

– Lucas Kot-Zaniewski
Nov 18 '18 at 2:09













because I don't have anything to use as a length other than the team string

– J. Java
Nov 18 '18 at 2:13





because I don't have anything to use as a length other than the team string

– J. Java
Nov 18 '18 at 2:13













You need to post this data structure because your trouble traversing it is hard to diagnose otherwise. Seems like you could use the length of players list to get a player.

– Lucas Kot-Zaniewski
Nov 18 '18 at 2:15







You need to post this data structure because your trouble traversing it is hard to diagnose otherwise. Seems like you could use the length of players list to get a player.

– Lucas Kot-Zaniewski
Nov 18 '18 at 2:15















I got the code to work after changing "team.length()" to "players.size()," letting me delete the team string

– J. Java
Nov 18 '18 at 2:19





I got the code to work after changing "team.length()" to "players.size()," letting me delete the team string

– J. Java
Nov 18 '18 at 2:19













I strongly suggest you visit oracle documentation on all the accessible properties of an object implementing list interface. Or anything else java related for that matter.

– Lucas Kot-Zaniewski
Nov 18 '18 at 2:20





I strongly suggest you visit oracle documentation on all the accessible properties of an object implementing list interface. Or anything else java related for that matter.

– Lucas Kot-Zaniewski
Nov 18 '18 at 2:20












1 Answer
1






active

oldest

votes


















1














I hope this solves your problem! Cheers.



public static PlayerScores findHighestScorer(List<PlayerScores> players) {
PlayerScores bestScore = null;

// loop through list...
for (PlayerScores score : players) {
if (bestScore == null) {
bestScore = score;
} else {
if (bestScore.getAverage() < score.getAverage()) {
bestScore = score;
}
}
}

return bestScore;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357261%2fjava-how-to-compare-elements-in-an-object-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I hope this solves your problem! Cheers.



    public static PlayerScores findHighestScorer(List<PlayerScores> players) {
    PlayerScores bestScore = null;

    // loop through list...
    for (PlayerScores score : players) {
    if (bestScore == null) {
    bestScore = score;
    } else {
    if (bestScore.getAverage() < score.getAverage()) {
    bestScore = score;
    }
    }
    }

    return bestScore;
    }





    share|improve this answer




























      1














      I hope this solves your problem! Cheers.



      public static PlayerScores findHighestScorer(List<PlayerScores> players) {
      PlayerScores bestScore = null;

      // loop through list...
      for (PlayerScores score : players) {
      if (bestScore == null) {
      bestScore = score;
      } else {
      if (bestScore.getAverage() < score.getAverage()) {
      bestScore = score;
      }
      }
      }

      return bestScore;
      }





      share|improve this answer


























        1












        1








        1







        I hope this solves your problem! Cheers.



        public static PlayerScores findHighestScorer(List<PlayerScores> players) {
        PlayerScores bestScore = null;

        // loop through list...
        for (PlayerScores score : players) {
        if (bestScore == null) {
        bestScore = score;
        } else {
        if (bestScore.getAverage() < score.getAverage()) {
        bestScore = score;
        }
        }
        }

        return bestScore;
        }





        share|improve this answer













        I hope this solves your problem! Cheers.



        public static PlayerScores findHighestScorer(List<PlayerScores> players) {
        PlayerScores bestScore = null;

        // loop through list...
        for (PlayerScores score : players) {
        if (bestScore == null) {
        bestScore = score;
        } else {
        if (bestScore.getAverage() < score.getAverage()) {
        bestScore = score;
        }
        }
        }

        return bestScore;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 '18 at 2:17









        Big LebowskiBig Lebowski

        1214




        1214






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357261%2fjava-how-to-compare-elements-in-an-object-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini