ArrayList finding maxValue Java!?
-
Hi everybody! I have a little dificculty with an arrayList in java. It should find the position with the highest score in an array containing members from a boule management system. I am looping through my list containing the values via a players variable that is counting of how many members the program already have! I have tried to code different solutions but It doesnt work! The program should get the position where the members score is and then compare it. My code so far: public static int showBestResults(ArrayList<BoulePlayer> player, int score, int players) { for(int i = 0; i < players; i++) { if((player.get(i).getScore() > score)) { score = (player.get(i).getScore()); } } System.out.println("Highest point " + score); return score; } It takes getscore from the BoulePlayer class where I create the objects!
-
Answer:
This one does what I THINK you want to do. public static int showBestResults(ArrayList<BoulePlayer> player) {int positionTemp=0,highScore=player.get(0).g… for(int i = 1;i<player.size();i++) if ( player.get(i).getScore()> highScore) {highScore = player.get(i).getScore(); positionTemp = i} System.out.println("Player with highest score was player #: "+positionTemp+" with a score of "+highScore); return highScore;} No need to send in a score. Just grab the score of the first one in the array and compare to the rest.
Sebas at Yahoo! Answers Visit the source
Other answers
you can find this! Something like: int score = 0; int p; for(int i = 0; i<player.size(); i++) { if(player.get(i).getScore())>score){ score = player.get(i).getScore(); p = i; } } System.out.println("Highscore: " + Score); System.out.println("by Player: " + p);
I am not sure why you need score and players as input, those can be simply extracted from the array right? i.e. player.size() and starting score should be zero. Something like: int score = 0; int p; for(int i = 0; i<player.size(); i++) { if(player.get(i).getScore())>score){ score = player.get(i).getScore(); p = i; } } System.out.println("Highscore: " + Score); System.out.println("by Player: " + p);
Magician
The for loop should run to the size of the array list. Not sure you need int players or int score in the parameters. I'd set score to 0 and then inside the for loop, set int temp to player.get(i).getScore(). Also, I usually throw in some extra println methods to see exactly what's going on, that might help.
Money_Dude
Related Q & A:
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- How to perform operation on Arraylist?Best solution by Stack Overflow
- How to save new objects into an arrayList?Best solution by stackoverflow.com
- How to pass an ArrayList from one class to another?Best solution by stackoverflow.com
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.