• Post Reply Bookmark Topic Watch Topic
  • New Topic

finding minimum/maximum  RSS feed

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any method already defined in Java that helps to comapre a series of numbers and find either the minimum or the maximum from this set?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, check out Math.min() and Math.max() in the java.lang package
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After re-reading your post, I think you're trying to compare more than 2 numbers at one time. The methods I mentioned only compare 2 numbers. Of course you could write your own method to compare a series that utilizes the Math.min() and Math.max() methods.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also look at Collections.min(), Collections.max(), and Collections.sort() that works on a large series (or collection) of objects since Math.min() & Math.max() only compares two numbers. Be sure not to confuse the java.util.Collections class (plural) with the java.util.Collection Interface (singular). Be sure to read the API docs for caveats on the natural sort order used by these methods.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a big fan of the REXX language which has optional arguments, so min() and max() allow any number of numbers. Or strings. Or whatever. Java doesn't support that kind of optional arguments, so min and max were sensibly written for exactly two numbers. I guess you could do something unattractive like:
x = min(a,min(b,min(c,min,d)));
Hmmm, nope, don't do that.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!