繁体   English   中英

查找数组列表中的最小数字 java

[英]Finding the smallest number in array list java

所以我有一些代码可以找到一系列点之间的距离。 一种方法使用欧几里得距离并且工作正常,另一种方法使用曼哈顿,我不知道为什么它不起作用。

我已将其设置为数组列表中第一个元素的距离对于两种方法都为零,因此应该打印图像 1 是匹配的。 然而,Manhattan 方法总是返回图像 31,无论我用多少不同的元素对其进行测试。 我已经仔细检查了数组列表中的元素,它应该返回图像 1。

有人有什么想法吗? 提前致谢

public void matchEuclidean(){
    for(int i = 0; i < numberimages; i++){
        distanceE[i][0] = weights[i][0] - testweights[0][0];
        distanceE[i][1] = weights[i][1] - testweights[0][1];
      }
      
    for(int i = 0; i < numberimages; i++){
        distanceEu[i] = (Math.pow(distanceE[i][0], 2)) + (Math.pow(distanceE[i][1], 2));
        distanceEu[i] = Math.sqrt(distanceEu[i]);
       }  
       
     
for (double no : distanceEu) {
     list.add(Double.valueOf(no));
     }
      
   double max= Collections.min(list);
   double min =  list.indexOf(max) + 1;    
 System.out.println("(euclidean) the unknown image matches image " + (min)); 


 }
 
 public void matchManhattan(){
     for(int i = 0; i < numberimages; i++){
        distanceM[i][0] = weights[i][0] - testweights[0][0];
        distanceM[i][1] = weights[i][1] - testweights[0][1];
      }
      
      for(int i = 0; i < numberimages; i++){
        distanceMu[i] = distanceM[i][0] + distanceM[i][1];
        
       } 
  
     for (double no : distanceMu) {
     listM.add(Double.valueOf(no));
     }
      
     double max= Collections.min(listM);
     double min =  listM.indexOf(max) + 1;    
     System.out.println("(Manhattan) the unknown image matches image " + (min)); 


 }

看起来您忽略了在曼哈顿距离中使用Math.abs function :

distanceMu[i] = Math.abs(distanceM[i][0]) + Math.abs(distanceM[i][1]);

没有它,你就没有一个有效的“距离” function:你可以得到负值,三角不等式不成立

int a[] = {6,22,33,12,44,9};

    int low = 0;
    
    for(int i = 1 ; i < a.length ; i ++ )

    {
        if(a[0] < a[i])
        {
            low = a[0];
        }
        else if(a[0] > a[i])
        {
            low = a[i];
            a[0] = low;
            
        }
    }
    System.out.println("The smallest number is the given array is : " + low);
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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