2014-02-20 42 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爲最後的指數。你應該改變你的代碼,取決於你想如何處理最後一個元素。

相關問題