File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes want a better code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "want a better code" Watch "want a better code" New topic

want a better code

Devesh Gurjar

Joined: Jan 24, 2013
Posts: 1
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

Joined: Aug 05, 2005
Posts: 3742
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

Joined: Aug 05, 2005
Posts: 3742
Edit: Whoops. Meant to edit my previous post, not quote it.
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8927

Devesh Gurjar wrote:Hello


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


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link:
subject: want a better code
jQuery in Action, 3rd edition