Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want a better code

 
Devesh Gurjar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have worked out a problem to find the winner and second runner up of a marathon.However, I would like to know a more efficient method to get the runner up of the race.Method which I have employed to accomplish this task is to remove the data of the winner of the race and then to determine the winner of our modified list.



[Added code tags - see UseCodeTags for details]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the object oriented way to do it is to create a class to hold a participant name and their time.
You also make this class implement the Comparable interface.
You then create an instance of this class for each person and add these instances to a List. You then sort this list after which the first entry in the list will be the winner and the second entry will be the runner up.

However, if none of that makes any sense to you yet, then the simple way to do it at the moment is to just add a second pair of variables to hold the name and time of the runner up and then set these in a similar way to how you are currently setting the winner's name and time in the min1 and name1 variables.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit: Whoops. Meant to edit my previous post, not quote it.
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devesh Gurjar wrote:Hello

Devesh,

1. Welcome to JavaRanch.
2. Please DontWriteLongLines. I've broken yours up this time.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic