Java:调用 Class 类型的数组来测试是否相等
Java: Invoking a Class-type Array to Test for Equality
我正在开发一个程序,该程序首先采用偶数双精度数组,然后按顺序创建第二个 (x, y) 坐标数组。然后,必须通过比较数组中每个点的 x 坐标和 y 坐标,将调用 PointArray 与参数 PointArray 进行比较。我从概念上知道我必须对每个数组进行排序并使用索引和定义的 equals 方法比较每个点,但我不知道如何引用调用 class 类型数组的点未作为参数给出。
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 能比文字更好地展示它。
我正在开发一个程序,该程序首先采用偶数双精度数组,然后按顺序创建第二个 (x, y) 坐标数组。然后,必须通过比较数组中每个点的 x 坐标和 y 坐标,将调用 PointArray 与参数 PointArray 进行比较。我从概念上知道我必须对每个数组进行排序并使用索引和定义的 equals 方法比较每个点,但我不知道如何引用调用 class 类型数组的点未作为参数给出。
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 能比文字更好地展示它。