Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Finding Multiple Equal Max Values in an Array

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Finding Multiple Equal Max Values in an Array

    Salutations, all! I'm taking my first Java class in college and working on a personal project on the side so this is not an assignment and not urgent in any way. I've been really impressed with reading the posts on this forum, particular the various guides people have posted. Also a special nod to KevinWorkman for helping me out last time, thanks again!

    I tried searching the forums for a similar thread but apparently "max" and "value" are too common to search for and "max value in an array" seemed to return many "array" threads but not any relevant ones. I apologize in advance if this is a redundant problem, all I've been able to find on the subject is how to find a single max value in the array.

    The Program
    I have an array of Player objects (created at the start of the program, can be any valid int in length) which have an int Number and int Score. I need to find the highest score in the array and record that Player's Number and Score. Here's my code right now:

           // Player with highest score wins
           for (counter = 0; counter < players.length; counter++)
                   {
                           System.out.println("Player" + players[counter].getNumber() + " 's final score is " + players[counter].getScore() );
     
                           if (players[counter].getScore() > winnerScore){
     
                                   winnerScore = players[counter].getScore();
                                   winner = players[counter].getNumber();
     
                                   }
     
                           else{
                           }
                   }
     
    System.out.println( "Player" + winner + " wins with " + winnerScore + " points!" );

    The Problem
    This seems to work how I want it to except it has no way of accounting for ties, i.e. when more than one player has the same score. Because the for loop goes through the array entry by entry the first player with the high score will always come out as the winner.

    Possible Solution
    I believe any solution to this problem requires creating a new array of Player objects for winning players equal to the original Player array's length, since it's possible for all players to be tied. I could use an if statement to move player objects to this new array (if their score was >= the current highest score) but since the for loop starts at the beginning of the array it would always get Player1 and would get other players that didn't necessarily have the highest score-just the highest to that point.

    If I could create a winning array and move Players objects with a Score => winnerScore to it while also removing any Player objects with a Score < the Score of the new Player object being added I believe that would work. In order to do that I would need to have another for loop go through the winner's array and find the max score there, then remove all Player objects with a lower Score? That seems like it's overly redundant to me.

    Another Possible Solution
    Sorry if these seem a bit stream-of-conscious but I'm just thinking of them as I post this message. If I had the code above find the winnerScore, I could have another for loop go through the array and assign all players with a Score equal to winnerScore to a new winner's array, then print every Player object's number and score in that array? Now that I've thought of it that seems like the right thing to do, but I'm wondering if it's possible to do it in a single loop.

    Unfortunately I do not have a Java editor on the computer I am at now or else I would try the latter method above which I do think will work if I'm thinking of it right. However I'd really like to keep the coding as simple as possible and I'm still unable to think of a single-loop method. Again this is just for fun so I don't mean to be a burden, but I'm excited because I got such a quick and helpful response last time.

    In any case thanks again to all the people who help out on this forum, it's great to know there's a place like this if something seriously important ever did come up.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    My Mood
    Sneaky
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Finding Multiple Equal Max Values in an Array

    What you could do is create an ArrayList of Player(s), add the first player to the array list. Iterate through the rest of the players and check if their score is greater than the score of the first entry in the array list, if it is, clear the array list and add the current player. Otherwise, if the current players score is equal to the score of the first entry in the array list, add them too. (Explaining in english is so much harder than diagrams or code!)

    This will let you store the whole Player object, and you could do some cool stuff around keeping a score history or stats for each player, etc.

    Hopefully that is what you mean

  3. The Following User Says Thank You to Tsarin For This Useful Post:

    AbsolutelyNobody (October 26th, 2011)

  4. #3

    Default Re: Finding Multiple Equal Max Values in an Array

    Using an ArrayList seems like overkill.

    I think you should use multiple methods:

    int getHighestScore()
    Player[] getWinners()
    void printWinners()

    Inside the getWinners() method, you create an array and add players to the new array if their score equals the high score.

    Inside printWinners(), you traverse the array created from the getWinner() method in a loop and print out what you need.

    Simple.

    No ArrayList or extra datastructures.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  5. The Following User Says Thank You to kenster421 For This Useful Post:

    AbsolutelyNobody (October 26th, 2011)

  6. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Finding Multiple Equal Max Values in an Array

    Thanks for the help you guys, I really appreciate it! I wound up doing a version of the alternate possible solution in my first post, which is pretty much the same thing both of you guys recommended (albeit I've not touched arrayLists yet and used an array). I think my problem was trying to do it all in a single For loop and single array, once I moved beyond that it was easy.

    Glad to know the people on this forum are so helpful! Thanks again, and good luck with everything!

  7. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding Multiple Equal Max Values in an Array

    Hi,
    I think for a better solution a generic class should be created for this to solve it using Reflection too.

    bean factory
    Last edited by abani; December 18th, 2011 at 01:58 AM.

Similar Threads

  1. Cant set an array value equal to another array value?
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 1
    Last Post: October 9th, 2011, 11:21 AM
  2. Replies: 5
    Last Post: May 24th, 2011, 11:39 AM
  3. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 08:57 PM
  4. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM
  5. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM

Tags for this Thread