如何比较两个双数并找到更小的数

时间:2013-08-30 07:10:03

标签: java compare double rectangles

这个类包含矩形列表,我需要找到面积最小的矩形。

我发现需要按区域比较矩形,但它具有双精度。 我知道我的比较记得最后一个,但是我们如何在这里检查呢?

代码:

   /**
    * Gets the Rectangle with the smallest area
    * @return the rectangle with the smallest area or null if
    * there are no rectangles
    */
   public Rectangle smallestArea()
   {        
       if (list.size() == 0) return null;

       Rectangle smallest = list.get(0);           
       double smallestArea = smallest.getWidth() * smallest.getHeight();

       for (int i = 1; i < list.size(); i++) {
           Rectangle next = list.get(i);
           double nextArea = next.getWidth() * next.getHeight();

           if ((nextArea - smallestArea) < 0) smallest = next;             
       }

       return smallest;
   }

如何解决这个问题?

7 个答案:

答案 0 :(得分:1)

您还应该更新smallestArea局部变量(代码中还有一行):

   public Rectangle smallestArea()
   {        
       if (list.size() == 0) return null;

       Rectangle smallest = list.get(0);           
       double smallestArea = smallest.getWidth() * smallest.getHeight();

       for (int i = 1; i < list.size(); i++) {
           Rectangle next = list.get(i);
           double nextArea = next.getWidth() * next.getHeight();

           if ((nextArea - smallestArea) < 0) { 
             smallest = next;         // <- Whenever you've updated smallest           
             smallestArea = nextArea; // <- Do not forget updating the smallestArea as well
           }  
       }

       return smallest;
   }

答案 1 :(得分:1)

您的算法不起作用,因为您还需要更新smallestArea

if ((nextArea - smallestArea) < 0) {
    smallest = next; 
    smallestArea = nextArea; // <<== Here
}

请注意,(nextArea - smallestArea) < 0是另一种说nextArea < smallestArea的方式,所以这看起来更干净:

if (nextArea < smallestArea) {
    smallest = next; 
    smallestArea = nextArea;
}

答案 2 :(得分:0)

我愿意

double smallest = Double.POSITIVE_INFINITY;

// in the loop.
if (smallest > next)
    smallest = next;

答案 3 :(得分:0)

除了记住最小的矩形之外,还需要记住它的大小。更改循环内的if

if (nextArea < smallestArea) {
    smallest = next;
    smallestArea = nextArea
}

答案 4 :(得分:0)

public Rectangle smallestArea()
{        
   if (list.size() == 0) return null;

   Rectangle smallest = list.get(0);           
   double smallestArea = smallest.getWidth() * smallest.getHeight();

   for (int i = 1; i < list.size(); i++) {
       Rectangle next = list.get(i);
       double nextArea = next.getWidth() * next.getHeight();

       if ((nextArea - smallestArea) < 0){
          smallest = next;             
          smallestArea = nextArea;
       }
   }

   return smallest;
}

答案 5 :(得分:0)

这个怎么样?我想你可以尝试这种方式

    List<Rectangle> list=new ArrayList<>();
    List<Double> areaList=new ArrayList<>();
    for(Rectangle r:list){
        areaList.add(r.getHeight()*r.getHeight());
    }
    Collections.sort(areaList);
    System.out.println("Smallest "+areaList.get(0));

答案 6 :(得分:-1)

尝试,

int retval = Double.compare(nextArea, smallestArea);

if(retval < 0)
{
  System.out.println("smallestArea is greater than nextArea");
  smallest = next;
}