0

我有一堆 int 值,比如 10、5、4、2、1。我有一堆Double值,List<Double>比如 0.65 等等。

如何通过此列表检查每个 Double 最接近的 int 值?

我的代码是用于 android 音乐应用程序的。这就是我最终解决它的方法。

            @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;
                        }
                    }
                }
          }
4

5 回答 5

3

使用Math.round(double)函数来舍入列表中的每个元素。Math.round(double)返回最接近 long参数的值,其中 ties( 0.5) 向上取整。但是,如果参数为NaN,则结果为0

所以

  1. 使用对给定的整数数组int[] a进行Arrays.sort(a)排序(如果尚未排序)
  2. 浏览每个双倍的列表x
  3. 求整数key = Math.round(x)
  4. 使用Arrays.binarySearch(a, key);其中a是一个int[]包含您的整数数组元素。negative index如果数组中不存在该元素,此函数可能会返回 a a。表示我们正在搜索的数组的negative index: (-(insertion point) - 1).插入点。

示例代码:

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]);

  }
于 2013-11-07T05:57:58.853 回答
2

首先,Math.round()按照其他人的建议使用来获得一个 int。然后,如果您的整数是有序的,请查看Arrays.binarySearch().

于 2013-11-07T06:01:40.447 回答
1

您确实需要尝试一些事情,然后在遇到问题时在此处发布。

但是,作为提示:对于您的每个双打,请查看您的整数列表。对于每个整数,计算双精度与该整数的距离。如果计算出的距离小于当前最小值,那就是新的最小值。

首先在纸上解决这些事情真的很有帮助。人类会怎么做?如果你能解决这个问题,那么你将它翻译成程序会容易得多。

于 2013-11-07T06:02:00.797 回答
1

尝试找到 的最小值listOfDoubles.get(i) - anyInteger,然后存储最小差异的索引i

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

只是想知道我将如何做到这一点。

于 2013-11-07T06:04:13.377 回答
0

Math.round(Double d)injava.lang.Math将取一个Double对象并将其四舍五入到最接近的位置Long,并用 ties ( .5) 向上取整。

请参阅:Oracle Java 文档

于 2013-11-07T05:58:47.690 回答