Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Easy way to get the Max value of array  (Read 1745 times)
0 Members and 1 Guest are viewing this topic.
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« on: July 21, 2008, 01:55:21 PM »

Code
GeSHi (java):
  1. [
  2. public class SimpleGettingMaxValue {
  3.  
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. int[] array = {21, 2, 32, 34, 11, 100, 132, 32, 32, 1000, 32, 3, -55};
  9. int maxNumber = 0;
  10.  
  11. for (Integer num : array) {
  12. if (maxNumber < num) {
  13. maxNumber = num;
  14. }
  15. }
  16. System.out.println(maxNumber);
  17.  
  18. }
  19.  
  20. }
Created by GeSHI 1.0.7.20
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #1 on: July 25, 2008, 08:47:42 PM »

Nice, although I prefer it this way

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. ArrayLargestNumber class
  3.  *****************
  4. By: deAppel http://www.engineeringserver.com
  5. Contact: info [@] engineeringserver.com
  6. Version: 26/07/2008
  7. "*****************
  8. Note: Array max/min number demo
  9. //----------------------------------------------------------------------*/
  10. import java.util.Arrays;
  11.  
  12. public class ArrayLargestNumber {
  13. public static void main(String[] args) {
  14. int[] intArray = {21, 2, 32, 34, 11, 100, 132, 32, 32, 1000, 32, 3, -55};
  15. int largestNumber = intArray.length-1;
  16. Arrays.sort(intArray);
  17. System.out.println("Largest number: " + intArray[largestNumber]);
  18. System.out.println("Smallest number: " + intArray[0]);
  19. }
  20. }
  21.  
Created by GeSHI 1.0.7.20

Output
Largest number: 1000
Smallest number: -55
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #2 on: July 27, 2008, 03:50:49 PM »

yeha I use that also, but sometimes you just need to make something yourself, even if it is simple algorithm.
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #3 on: July 27, 2008, 04:01:31 PM »

yeha I use that also, but sometimes you just need to make something yourself, even if it is simple algorithm.

True, thanks for the tips though i really appriciate it. If you have more let us know  Very nice
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here