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 7 of 7

Thread: How to get the highest value of an array of integers?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to get the highest value of an array of integers?

    I am trying to write a method that returns the busiest hour in a logAnalyzer class that read web server data and analyze hourly access patterns and stores them in an array. My problem is, in order to get the busiest hour, I need to go through the hourCounts array to find the element with the biggest count.
    Any suggestions please?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to get the highest value of an array of integers?

    Quote Originally Posted by bihlas View Post
    I need to go through the hourCounts array to find the element with the biggest count.
    Have you tried doing exactly that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to get the highest value of an array of integers?

    Going through all the elements of the array I am using a for loop, getting each count every time I loop through the array. how to specify in the loop body which from all the counts is the biggest in order to return the associated hour with that count.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to get the highest value of an array of integers?

    I'm not really sure what you're asking. How would you do this by hand?

    Say you have 100 index cards labeled 1-100 on the front and a random number on the back. Your job is to find the card with the highest random number, and then tell somebody else its index and the number. How would you do that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to get the highest value of an array of integers?

    One technique is to set the variable with the max value to the first element in the array, and then compare all the other elements in the array against it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to get the highest value of an array of integers?

    /**
         * Returns the busiest hour of the day.
         */
        public int busiestHour()
        {
            int theBusiestHour = 0;
            // The hourCounts array has length = 24.
            for(int hour = 0; hour < hourCounts.length; hour++) {
     
            }
            return theBusiestHour;
        }

    To make it more clear about what I want.

    Norm I cant set the variable with the max value to the first element of the array because it would always return as the busiest hour to be midnight. I need help to understand how to find the the biggest count in the array in order to assign the associated hour to theBusiestHour variable.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to get the highest value of an array of integers?

    I don't think you understood Norm. Use a variable as the highest value set to the first value in the array:

    int highestValue = hourCounts( 0 );
    int highestElement = 0;

    Compare each of the remaining array elements to highestValue using a loop that examines each element of the array, and if a new higher value is found, update the highestValue AND highestElement variables. Return the highestElement value, because that points to the highestValue in the array hourCounts().

    Good luck!

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    bihlas (July 2nd, 2014)

Similar Threads

  1. Creating method to print array of integers
    By kferd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2014, 08:14 PM
  2. Turning the letters in a character array to integers.
    By taze in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 7th, 2013, 01:32 AM
  3. how to sort an array of integers(eg using barcode)
    By jovita mateus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2013, 02:09 AM
  4. Printing highest number of array. Can't find my error.
    By Praetorian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2013, 11:46 PM
  5. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM