简体   繁体   中英

Closest int that a Double is to in Java

I have a bunch of int values like 10, 5, 4, 2, 1. And I have a bunch of Double values in a List<Double> like 0.65 etc etc.

How can I go through this list a check which int value each Double is closest to?

My code was for an android music app. Here is how I ended up solving it.

            @Override
            public void onClick(View v) {
                String[] noteNames = new String[9];
                double[] notes = new double[9];
                double crotchet = 60.0 / Long.parseLong(setBPM.getText().toString());
                double quaver = crotchet / 2.0;
                double tripletQuaver = crotchet / 3.0;
                double dottedCrotchet = crotchet + quaver;
                double semiquaver = quaver / 2.0; 
                double dottedQuaver = quaver + semiquaver;
                double minum = crotchet * 2.0;
                double dottedMinum = minum + crotchet;
                double semibreve = minum * 2.0;
                notes[0] = semiquaver;
                notes[1] = tripletQuaver;
                notes[2] = quaver;
                notes[3] = dottedQuaver;
                notes[4] = crotchet;
                notes[5] = dottedCrotchet;
                notes[6] = minum;
                notes[7] = dottedMinum;
                notes[8] = semibreve;
                noteNames[0] = "semiquaver";
                noteNames[1] = "tripletQuaver";
                noteNames[2] = "quaver";
                noteNames[3] = "dottedQuaver";
                noteNames[4] = "crotchet";
                noteNames[5] = "dottedCrotchet";
                noteNames[6] = "minum";
                noteNames[7] = "dottedMinum";
                noteNames[8] = "semibreve";
                for(double l : notes) {
                    System.out.println(l);
                }
                double[] tempCurrentDiff = new double[9];
                double[] currentDiff = new double[9];
                for (Double d : beats) {
                    for (int i = 0; i < 9; i++) {
                        currentDiff[i] = Math.abs(notes[i] - d);
                        tempCurrentDiff[i] = Math.abs(notes[i] - d);
                    }
                    Arrays.sort(tempCurrentDiff);
                    for (int i = 0; i < 9; i++){
                        if (currentDiff[i] == tempCurrentDiff[0]) {
                            System.out.println(noteNames[i]);
                            notesView.append(noteNames[i] + " ");
                            break;
                        }
                    }
                }
          }

Use Math.round(double) function to round your each element of your list. Math.round(double) Returns the closest long to the argument, with ties( 0.5 ) rounding up. However, If the argument is NaN , the result is 0 .

So ,

  1. sort your given integer array int[] a using Arrays.sort(a) , if not already sorted
  2. go through the list for each double x
  3. find integer key = Math.round(x)
  4. use Arrays.binarySearch(a, key) ; where a is an int[] containing your integer array elements. This function might return a negative index if the element is not present in the array a . The negative index: (-(insertion point) - 1). representing the insertion point of the array we are searching in.

sample code:

double doub[] = {0.5, 2.6, 3.7, 1.7, 4.6};
  long a[] = {1, 4, 3}; //

  Arrays.sort(a);
  for(double x : doub)
  {
      long key = Math.round(x);
      int index = Arrays.binarySearch(a, key);
      if(index < 0) // element wasn't found
           index = - index -1;
      if(index > a.length -1) // key is greater than the maximum element 
         index = a.length - 1;

      System.out.println("double: "+x+" rounded key: "+key+" value: "+a[index]);

  }

First, use Math.round() as the others suggest to get an int. Then, if your ints are order, look at Arrays.binarySearch().

You really need to try something then post here when you have a problem.

However, as a hint: For each of your doubles, go through your integer list. For each integer, compute the distance of your double to that integer. If the computed distance is smaller than the current minimum, that's the new minimum.

It really helps to work these kinds of things out on paper first. How would a human do it? If you can work that out, then it will be much easier for you to translate it to a program.

Try to find smallest value of listOfDoubles.get(i) - anyInteger , then store smallest difference's index i

if (difference < min) {
    do something...
}

Just an idea of how I would do that.

Math.round(Double d) in java.lang.Math will take a Double object and round it to the nearest Long with ties ( .5 ) rounding up.

See: Oracle Java Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM