我正在开发一个程序,该程序首先采用偶数 double 组,然后依次创建第二个 (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 方法。
最佳答案
当您调用对象上的方法时,例如:
something.myMethod(argObject);
在myMethod
内,您可以将something
(调用该方法的对象)作为this
对象引用。
我希望this tuto比言语更能证明这一点。
关于Java:调用类类型数组来测试相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40923993/