Best way to use contains in an ArrayList in Java?

后端 未结 5 724
甜味超标
甜味超标 2021-01-12 05:51

I have an ArrayList in Java which is made up of a type containing two strings and an integer. I can successfully test if one element of this ArrayList equals another but I f

5条回答
  •  -上瘾入骨i
    2021-01-12 06:06

    My guess is that you've only written a "strongly typed" equals method instead of overriding equals(Object). In other words, if you've got:

    public boolean equals(Foo f)
    

    you need

    public boolean equals(Object o)
    

    as well to override Object.equals.

    That would fit with "equals works but contains doesn't because your tests probably call the strongly-typed equals, but ArrayList doesn't.

提交回复
热议问题