This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to find the maximum value in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find the maximum value in an array" Watch "How to find the maximum value in an array" New topic
Author

How to find the maximum value in an array

Uthpala Hathnagoda
Greenhorn

Joined: Feb 03, 2011
Posts: 1
I'm just a new to JAVA.
can you help me to find the maximum/minimum value in an array.
Thnx
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Do you know how to loop through an array ?


[My Blog]
All roads lead to JavaRanch
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hi, Uthpala Hathnagoda Welcome to JR.
You can achieve this in many ways- by using for loop (you may be done this in other languages like c,c++). Or there is one method in an Array name as sort(int[] arr). Using this method you can Sorts the array of into ascending numerical order. Once you get sorted array it is easy to find out max and min element in that array.


Life is easy because we write the source code.....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I think the idea is to find the maximum value without sorting. this is a common homework task for beginners. Please, Uthpala Hathnagoda, show us what you have got so far.

If you have got nowhere, get a pencil and paper and a large eraser. The eraser is by far the most useful piece of hardware . Write out an unsorted array of numbers. Write a maximum variable, give it an initial value, and write down how you think you can iterate through the array and get maximum to be equal to the largest value. Leave spaces after each line . . .



. . . then go back and enhance your algorithm by adding a minimum value.

The following sentences show what you are aiming for: all words of one syllable.
Once you have made that work well and work right, write down what you have done in small words. Then you can change what you wrote into code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
And welcome to the Ranch
 
GeeCON Prague 2014
 
subject: How to find the maximum value in an array