title: get closest value to a number in array
body: <p>I have an array of positive/negative ints</p>
  
  <pre><code>int[] numbers = new int[10];
  numbers[0] = 100;
  numbers[1] = -34200;
  numbers[2] = 3040;
  numbers[3] = 400433;
  numbers[4] = 500;
  numbers[5] = -100;
  numbers[6] = -200;
  numbers[7] = 532;
  numbers[8] = 6584;
  numbers[9] = -945;
  </code></pre>
  
  <p>Now, I would like to test another int against this array, and return the number that is closest to the int.</p>
  
  <p>For example if I used the number <code>490</code> i would get back item #4 from numbers <code>500</code> what is the best way to do something like this?</p>
  
  <pre><code>int myNumber = 490;
  int distance = 0;
  int idx = 0;
  for(int c = 0; c &lt; numbers.length; c++){
      int cdistance = numbers[c] - myNumber;
      if(cdistance &lt; distance){
          idx = c;
          distance = cdistance;
      }
  }
  int theNumber = numbers[idx];
  </code></pre>
  
  <p>That doesn't work. Any suggestions on a good method to do this?</p>