如何在Java中使用ArrayList?

时间:2014-10-01 12:52:15

标签: java arraylist

我是新尝试学习编程和java,我目前在一个程序中工作,但我需要帮助启动它,如果有人能帮助我,我将非常感激。

我必须实现一个包含Point2D.Double对象数组列表的IrregularPolygon类。

Point2D.Double类定义以double精度指定的点,表示(x,y)坐标空间中的位置。例如,Point2D.Double(2.5,3.1)构造并初始化坐标(2.5,3.1)处的点。

我将使用此声明作为我实验室工作的起点。

import java.awt.geom.*;     // for Point2D.Double
import java.util.ArrayList; // for ArrayList

public class IrregularPolygon {
   private ArrayList <Point2D.Double> myPolygon;

   // constructors
   public IrregularPolygon() { }

   // public methods
   public void add(Point2D.Double aPoint) { }

   public double perimeter() { }

   public double area() { }
}

我想了解一些关于如何编写计算周长和多边形面积的方法的技巧。计算周长,计算相邻点之间的距离,总计距离。带角的多边形区域是绝对值:

3 个答案:

答案 0 :(得分:0)

我建议Math.hypot(point1.getX() - point2.getX(), point1.getY() - point2.getY())表示相邻点的距离。对于周长(正如你所说的那样)总结所有距离(不要忘记添加从最后一点到第一点的距离)。有关该地区,请参阅How do I calculate the surface area of a 2d polygon?

答案 1 :(得分:0)

我希望你能发现这个例子很有用:

import java.awt.geom.*;
import java.util.ArrayList;

public class IrregularPolygon {
    private ArrayList<Point2D.Double> myPolygon;

    // constructors
    public IrregularPolygon() {
        this.myPolygon = new ArrayList<Point2D.Double>();
    }

    // public methods
    public void add(Point2D.Double aPoint) {
        this.myPolygon.add(aPoint);
    }

    public double perimeter() {
        // compute perimeter
        return 0;
    }

    public double area() {
        // compute area
        return 0;
    }

    @Override
    public String toString() {
        String result = "IrregularPolygon [";
        for (Point2D.Double point : myPolygon) {
            result += "|X: " + point.x + ". Y: " + point.y + "|";
        }
        result += "]";
        return result;
    }

    public static void main(String[] args) {
        IrregularPolygon irregularPolygon = new IrregularPolygon();
        Point2D.Double point1 = new Point2D.Double(0, 2);
        Point2D.Double point2 = new Point2D.Double(2, 4);
        Point2D.Double point3 = new Point2D.Double(3, 5);

        irregularPolygon.add(point1);
        irregularPolygon.add(point2);
        irregularPolygon.add(point3);

        System.out.println(irregularPolygon.toString());
    }

}

您可以在toString()方法中找到如何在Java中使用ArrayList以及add()方法,因此方法perimeter()和{{1}的实现只是取决于你的规格。你只需要推断那些方法来完成课程。

希望它有所帮助。

Clemencio Morales Lucas。

答案 2 :(得分:0)

这是我到目前为止所做的:

import java.awt.geom.*;   
import java.util.ArrayList; 

public class IrregularPolygon {
private ArrayList <Point2D.Double> myPolygon;


public IrregularPolygon() 
{ 
myPolygon = new ArrayList < Point2D.Double > ();
}

public void add(Point2D.Double aPoint)
{
myPolygon.add(aPoint);
}

public void print()
{
for (Object element : myPolygon)
{
      System.out.println(element);
  }
}

public double distance(Point2D pt)
{
   double distance = 0;
   x.distance(y);
   return distance;
}

public double perimeter()
{
double distance = 0;


for(int x = 0; x < aPoint; x++){  // cords given in groups might have to / by 2
    get.point();
    if(myPolygon[] != null){
         get.nextPoint();
          }
      else{
          distance += thePoint.distance(nextPoint)
          }
}

return distance;
}

public double area()
{ 
double area = 0;

return area;
}
}

//that's irregular polygon. this is polygon application

import javax.swing.*;
import java.awt.geom.Point2D;

public class PolygonApplication
{
public static void main() {
String userInput;
int pointNumber = 1;
Point2D.Double nextPoint;
IrregularPolygon myPolygon = new IrregularPolygon();

do  {
     userInput = JOptionPane.showInputDialog("Enter x coordinate for point  " + pointNumber);
     if (userInput != null) {
     double x = Double.parseDouble(userInput);
     userInput = JOptionPane.showInputDialog("Enter y coordinate for point  " + 
             pointNumber);
if (userInput != null) {
                  double y = Double.parseDouble(userInput);
                  nextPoint = new Point2D.Double(x,y);
                  myPolygon.add(nextPoint);
                  pointNumber += 1;
}
}

} 
while (userInput != null);

myPolygon.print();
}
}