Java:调用类类型数组来测试相等性

标签 java arrays class equals

我正在开发一个程序,该程序首先采用偶数 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/

相关文章:

java - 读取文件并检查行是否以分号结尾

c++ - 为什么我不能删除这个数组元素的指针值?

arrays - Perl next 如果正则表达式存储在数组中

java - 为什么这个对象数组中的每个元素都会被最后一个对象覆盖?

c++ - 类名作为 vector 的对象和 push_back 函数的参数

java - 在 SWT 界面未运行时显示 MessageBox

java - 如何从单个数据定义生成 Java 和 PHP 中的实体?

java - 非常非常粗糙的排序

javascript - 与 Closure Compiler 一起工作的 JavaScript 类的实现

java - 有没有办法使用 Rally Rest API 通过标签获取某个项目的所有用户故事?