Java - How To Compare Elements In An Object List
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
add a comment |
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
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
add a comment |
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
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
java object
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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;
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 18 '18 at 2:17
Big LebowskiBig Lebowski
1214
1214
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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