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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
D'OH.
int diff = (int) Math.abs(a - b);
ASKER
for (int i=0;i<closer.size();i++)
{
int temp = Math.abs((Integer)closer.g et(i));
closer.remove(i);
closer.add(temp);
}
Am I on the right track with this?
First getting them and then converting them into absolutes?
{
int temp = Math.abs((Integer)closer.g
closer.remove(i);
closer.add(temp);
}
Am I on the right track with this?
First getting them and then converting them into absolutes?
ASKER
I'm always dealing with integers here so is there a need for Math.abs, or am I misunderstanding something?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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("Closes t: "+bestdiff);
Does that look right to you?!
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("Closes
Does that look right to you?!
ASKER
No. It's not right. :-)
Neither's this. :-(
System.out.println("Closes t: "+info2[(Integer)closer.ge t(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?
Neither's this. :-(
System.out.println("Closes
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?
ASKER
objects: Do you have any idea how I could get the correct output which corresponds to the array?
ASKER
Never mind...
for (int i=0; i < stations.length; i++)
{
if (stations[i].contains(sear chTerm))
{
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,best diff)-Math .min(depar tIndex,bes tdiff));
This provides the answer I'm looking for.
Thanks very much again objects!
for (int i=0; i < stations.length; i++)
{
if (stations[i].contains(sear
{
System.out.println("Found:
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,best
This provides the answer I'm looking for.
Thanks very much again objects!
no worries (sorry, I stepped out for lunch)
ASKER
lol! It's ok!
It's 05:20 here! What I wouldn't give for a Subway about now!! :-)
It's 05:20 here! What I wouldn't give for a Subway about now!! :-)
We got spoiled with Yum Cha today :)
ASKER