Link to home
Start Free TrialLog in
Avatar of DanBAtkinson
DanBAtkinson

asked on

Return the closest number in a list to a given value

Hi there.

The question is simple really.

I have a list which adds a integers in a loop to it based on the outcome of certain conditions. At the end of that loop, I want to find out which is the closest number in that list to a value I already have.

How would I do that?

Example:

int myInt = 13;

myList (after going through the loop) = {1,16,20,31,2,12,4}

getClosestNumber(myInt, myList)

public int getClosestNumber(int value, List valueList)
{
  run marvellous check I don't know how to do.

  return int; //In this case 12. If there are two numbers (ie, if 14 is also in the list, return the lowest number).
}

Thanks in advance
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanBAtkinson
DanBAtkinson

ASKER

Thanks. Looking at abs in the API, how can I determine the difference between two numbers if I can only pass one?
D'OH.
int diff = (int) Math.abs(a - b);
for (int i=0;i<closer.size();i++)
{
      int temp = Math.abs((Integer)closer.get(i));
      closer.remove(i);
      closer.add(temp);
}

Am I on the right track with this?

First getting them and then converting them into absolutes?
I'm always dealing with integers here so is there a need for Math.abs, or am I misunderstanding something?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK.

So:

if (closer.size()>0)
{
      int bestdiff = Integer.MAX_VALUE;
      int closest = -1;
      for (int i=0;i<closer.size();i++)
      {
            int n = (Integer)(closer.get(i));
            int diff = Math.abs(departIndex - n);
            if (diff <bestdiff)
            bestdiff = diff;
      }
      System.out.println("Closest: "+bestdiff);



Does that look right to you?!
No. It's not right. :-)
Neither's this. :-(

System.out.println("Closest: "+info2[(Integer)closer.get(bestdiff)]);

I rewrote it to this because I need to return a value from an array (whose index corresponds to the value in the 'closer' list.

Does this make sense, and am I going in the right direction?
objects: Do you have any idea how I could get the correct output which corresponds to the array?
Never mind...


        for (int i=0; i < stations.length; i++)
        {
           if (stations[i].contains(searchTerm))
                    {
                               System.out.println("Found: "+interchanges[i]);                        
                              closer.add(i);
                    }
        }
                      if (closer.size()>0)
                    {
                        int bestdiff = Integer.MAX_VALUE;
                        for (int i=0;i<closer.size();i++)
                        {
                              int n = (Integer)(closer.get(i));
                              int diff = Math.abs(departIndex - n);
                              if (diff <bestdiff)
                              {
                                    bestdiff = diff;
                              }
                        }                        
                        int interchangeStation = (Math.max(departIndex,bestdiff)-Math.min(departIndex,bestdiff));


This provides the answer I'm looking for.

Thanks very much again objects!
no worries (sorry, I stepped out for lunch)
lol! It's ok!

It's 05:20 here! What I wouldn't give for a Subway about now!! :-)
We got spoiled with Yum Cha today :)