Solved

java lists

Posted on 2004-09-29
8
218 Views
Last Modified: 2010-04-17
how can I find the second highest value in a list of integers? and also the second lowest ?
0
Comment
Question by:rwallacej
  • 2
  • 2
8 Comments
 
LVL 1

Assisted Solution

by:djspinnet
djspinnet earned 200 total points
ID: 12179369
to find the second highest,

declare two variables max and second_max

1) traverse the list
2) store the first integer in max
3) if the next int (let's call it x) is larger than max, assign second_max = max; and max = x;
4) continue traversing the whole list and performing the same operation
5) return second_low

same goes for second lowest, just change >= to <=
0
 
LVL 1

Accepted Solution

by:
csaba_b earned 300 total points
ID: 12180266
Hi djspinnet!
your solution won't work for the following list: 2, 1 ... sorry

my proposal is:

suppose the list contains at leas 2 members and in case when two members has the highest value, the second highest equals the highest :)

int max = ((Integer)list.next()).intValue();
int max2 = ((Integer)list.next()).intValue();
int mxc = max2;
while(true) {
  if(mxc>=max) {
    mx2 = max;
    max = mxc;
  } else if(mxc>max2) {
    max2 = mxc;
  }
  if(!list.hasNextt()) {
    break;
  }
  mxc = ((Integer)list.next()).intValue();
}

for the second lowest you just replace > with <

csaba
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12180827
If speed is not an issue, just sort the list and grab the 2nd and 2nd from last item from the list
        Collections.sort(l);
        small=l.get(1);
        large=l.get(l.size()-2);
0
 
LVL 1

Expert Comment

by:djspinnet
ID: 12188464
actually, my solution did work - i implemented it once ;)
0
 
LVL 1

Expert Comment

by:csaba_b
ID: 12402861
To djspinnet:

Have you tried your algorithm with the list I mentioned? If yes, and the result was correct. you can accept the points offered.

I don't think so ...

Csaba
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 27
Delphi: barcode reading on android platform 1 51
Query group by data in SQL Server - cursor? 3 49
Vector, list Questions R 6 32
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question