简体   繁体   English

检查列表是否包含对象-Java

[英]checking if list contains the object - java

I am writing the image processing program, and I have a problem with a list. 我正在编写图像处理程序,列表有问题。

I have a list with Points. 我有一个积分列表。

     class Point1{
        private int x;
        private int y;

        Point1(int x,int y)
        {
            this.x = x;
            this.y = y;
        }

       int getX(){ return this.x; }
       int getY() {return this.y; }
    }

    ArrayList<Point1> list = new ArrayList();

And now, I create new Point, for example new Point(4,3); 现在,我创建一个新的Point,例如new Point(4,3); I want to check if there is a point in my list which has the same coordinates. 我想检查列表中是否有一个坐标相同的点。 The problem is that 问题是

    list.contains(Object a) 

is checking if if on my list there is a particular object. 正在检查我的列表中是否有特定对象。 It will work if I put 如果我放它会工作

    Point1 first = new Point(1,1);  
    list.add(first);
    list.contains(first) // and this is true

but: 但:

    Point second = new Point(2,2);
    list.add(second);
    list.contains(new Point(2,2)); <- false

how can I check it? 我该如何检查?

You need to override equals on your Point class. 您需要在Point类上重写equals Your Point class must be responsible for determining whether another Point object is "equal" to the current object. 您的Point类必须负责确定另一个Point对象是否“等于”当前对象。 An ArrayList will call equals to determine if the object passed in to contains is "equal" to any item in the list. ArrayList将调用equals以确定传入的contains对象是否“等于”列表中的任何项目。

If you don't override equals , then Point will inherit equals from Object , which will simply see if it's the same exact object. 如果不重写equals ,则Point将继承Object equals ,该Object将简单地查看它是否是相同的对象。 That's why your first code "works", because you are re-using first . 这就是为什么你的第一个代码“作品”,因为你重新使用first It also explains why your second code doesn't "work", because you used a different object. 它还说明了为什么您的第二个代码不起作用,因为您使用了另一个对象。

Also, if you override equals , it's best to override hashCode also (and vice versa). 另外,如果您覆盖equals ,那么最好也覆盖hashCode (反之亦然)。

List determines whether two objects are equal by the default equals() method extended from Object . List通过Object扩展的默认equals()方法确定两个对象是否相等。 The default equals() method is just determine whether the two object has the same domain value (I think it is the memory address). 默认的equals()方法只是确定两个对象是否具有相同的域值(我认为这是内存地址)。 And it can be printed by toString() . 它可以通过toString()打印。 Thus is you wanna to determine "equals" only by the values in this object are the same, you need to override equals() . 因此,您是否想仅通过此对象中的值相同来确定“等于”,您需要重写equals() you can code as below: 您可以编写如下代码:
@Override public boolean equals(Object other) { if(this == other) return true; if(other == null) return false; if(getClass() != other.getClass()) return false;
Point1 test = (Point1)other; if(this.x == test.getX() && this.y == test.getY()) return true; return false; }

And you can get the result you want: 您可以获得所需的结果:
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM