【发布时间】:2016-12-02 02:56:30
【问题描述】:
我正在开发一个程序,该程序首先采用偶数数组,然后依次创建第二个 (x, y) 坐标数组。然后,必须通过比较数组中每个点的 x 坐标和 y 坐标来将调用 PointArray 与参数 PointArray 进行比较。我从概念上知道我必须对每个数组进行排序并使用 indeces 和定义的 equals 方法比较每个点,但是我不知道如何引用调用类类型数组的一个点,而不是作为一个参数。
private double x;
private double y;
public Point(double x_coord, double y_coord)
{
x = x_coord;
y = y_coord;
}
public boolean equals(Point anotherPoint)
{
if(x == anotherPoint.x && y == anotherPoint.y)
{
return true;
}
return false;
}
int count;
private Point[] points = new Point[count];
public PointArray(double[] doubleArray)
{
count = (doubleArray.length) / 2;
if(doubleArray.length % 2 == 0)
{
count = (doubleArray.length) / 2;
for(int i = 0, j = 0; i < count; i++, j += 2)
{
double x = doubleArray[j];
double y = doubleArray[j + 1];
points[i] = new Point(x, y);
}
}
else
{
System.out.println("Error: The given array must be even.");
}
}
public boolean equals(PointArray anotherPointArray)
{
double x = 0;
double y = 0;
double xAnother = 0;
double yAnother = 0;
Point newPoint = new Point(x, y);
Point newAnotherPoint = new Point(xAnother, yAnother);
anotherPointArray.sort();
anotherPointArray.newPoint;
for(int i = 0; i < points.length; i++)
{
for(int j = 0; i < anotherPointArray.length; j++)
{
if(newPoint.equals(newAnotherPoint))
{
return true;
}
}
}
return false;
}
编辑:澄清一下,我的问题是我不知道如何使用 PointArray 对象设置 Point equals 方法。
【问题讨论】:
-
你的意思是你不知道如何在
equals函数中访问你的points数组?? -
@J.Baoby 你问的是哪个equals?如果您的意思是等于(PointArray anotherPointArray),那么是的,我遇到了麻烦。但我要问的是如何声明这两个数组。例如,我认为“testPointArray1.equals(testPointArray2)”将是我调用该方法的方式,在这种情况下 anotherPointArray 是 testPointArray2。但是,布尔值上下文中的 tesrPointArray1 是什么?