2014-02-20 37 views
0

我想弄清楚从area方法调用数组的正确方法,然后应该计算面积给出的观点。不知道从每个数组中选择特定x和y坐标的正确方法是什么。表达式的类型必须是数组类型,但它解析为ArrayList <Point2D.Double>

MyPolygon类

import java.util.ArrayList; 
import java.awt.geom.Point2D; 
import java.awt.geom.Point2D.Double; 

/** 
* A class that represents a geometric polygon. Methods are provided for adding 
* a point to the polygon and for calculating the perimeter and area of the 
* polygon. 
*/ 
class MyPolygon { 

    // list of the points of the polygon 
    private ArrayList<Point2D.Double> points; 

    /** 
    * Constructs a polygon with no points in it. 
    */ 
    public MyPolygon() { 
     points = new ArrayList<Point2D.Double>(); 
    } 

    /** 
    * Adds a point to the end of the list of points in the polygon. 
    * 
    * @param x 
    *   The x coordinate of the point. 
    * @param y 
    *   The y coordinate of the point. 
    */ 
    public void add(double x, double y) { 
     points.add(new Point2D.Double(x, y)); 
    } 

    /** 
    * Calculates and returns the perimeter of the polygon. 
    * 
    * @return 0.0 if < 2 points in polygon, otherwise returns the sum of the 
    *   lengths of the line segments. 
    */ 
    public double perimeter() { 

     if (points.size() < 2) { 
      return 0.0; 
     } 

     int i = 0; 
     double d = 0; 
     double total = points.get(0).distance(points.get(points.size() - 1)); 

     while (i < points.size() - 1) { 
      Point2D.Double point1 = points.get(i); 
      // double x = point1.x; 
      // double y = point1.y; 
      Point2D.Double point2 = points.get(i + 1); 
      // double x1 = point2.x; 
      // double y1 = point2.y; 

      d = point1.distance(point2); 
      // d = Math.sqrt(Math.pow(x1 - x,2) + Math.pow(y1 - y, 2)); 

      total = total + d; 
      i++; 

     } 
     return total; 

    } 

    /** 
    * Calculates and returns the area of the polygon. 
    * 
    * @return 0.0 if < 3 points in the polygon, otherwise returns the area of 
    *   the polygon. 
    */ 
    public double area() { 

     int i = 0; 
     double a = 0; 
     double total = 0; 
     total = total + a; 

     if (points.size() < 3) { 
      return 0.0; 
     } 

     for (int m = 0; m < points.size(); m++) { 

      total = total + (points[m].x() * points[m + 1].y()) - (points[m].y() * points[m + 1].x()); 
     } 
     return 0.5 * total; 
    } 

} 

Tester类

class PolygonTester { 

    public static void main(String args[]) { 
     MyPolygon poly = new MyPolygon(); 
     poly.add(1.0,1.0); 
     poly.add(3.0,1.0); 
     poly.add(1.0,3.0); 
     System.out.println(poly.perimeter()); 
     System.out.println(poly.area()); 
    } 

} 
+0

具体来说,它的问题即将到来的for循环。总数=总数+(问题) – user3308067

回答

1

您的标题实际上已经解决。您使用points[m]这是数组表示法。但是points不是一个数组。这是一个列表。改为使用points.get(int i),正如您在perimeter()中所做的那样。

0

你将会在列表中跑出界限。你的for循环继续,而size()。但是,您在计算中访问m + 1。因此,如果列表包含5个元素,并且m = 4(4 < 5)因此保持循环,您将访问m + 1,这是5.您没有索引5,因为这些列表基于0。

此外,代码可能不会编译,因为您使用数组语法来访问列表。你应该说points.get(m)

0

解决的办法很简单,是由您的标题送人(我假设是一个编译器错误。)

你是治疗points作为一个数组,它不是。您访问ArrayList的元素的方式稍有不同:使用points.get(m)而不是points[m]。如果您在area中进行了更改,它将起作用。

0

ArrayLists不是数组。它们是使用get(int)方法编制索引的对象。
无论你有points[m]或类似的东西,用points.get(m)代替它。然后,行会变成:

total = total + (points.get(m).x() * points.get(m + 1).y()) - (points.get(m).y() * points.get(m + 1).x()); 

这应该清理这个问题,但你仍然可能在循环的最后一次迭代得到IndexOutOfBoundsException,因为你将试图索引m + 1时,m为最后的指数。你应该改变你的代码,取决于你想如何处理最后一个元素。

相关问题