• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding the biggest number using java  RSS feed

 
praveen kumar velagambeti
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]Add code tags. CR[/edit]
[ August 01, 2008: Message edited by: Campbell Ritchie ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could loop through this array checking if the current number was bigger than the last. If it was, assign it to your biggest number variable. Or you could sort the array and grab the first/last element as is appropriate.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sort these numbers into TreeSet...then take the last element from TreeSet

Hope This Helps
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

What's wrong with the suggestion you originally posted? It works.

Actually there are three improvements I would suggest:
  • The name of the class "SampleProgram" does not describe what the class does. Suggest you change it.
  • The static field max is unnecessary; use a local variable instead.
  • The enhanced for loop (for-each loop) would be better than an ordinary for loop if you are using Java5 or Java6.
  • Apart from that, it is a perfectly acceptable maximum-finding application. You are already doing what Paul suggested, and using a TreeSet is probably beyond the stage you have reached in learning Java.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!