我们通常定义一个ArrayList,然后判断其中是否包含某个对象或元素

例如:

ArrayList list=new ArrayList();

if(list.contains(product)){

...

}

这样做contains是不起作用,需要在Product类中重写equals方法

@override

public boolean equals(Object obj){

  if(obj instanceOf Product){

    Product temp=(Product)obj;

    return temp.getId()==this.getId();

  }

  return super.equals(obj);

}

相关文章:

  • 2022-01-07
  • 2022-01-17
  • 2021-06-15
  • 2022-12-23
  • 2022-01-23
  • 2021-09-17
  • 2022-02-19
  • 2022-02-12
猜你喜欢
  • 2022-01-08
  • 2022-02-14
  • 2022-01-19
  • 2021-12-29
  • 2021-12-17
  • 2022-01-05
相关资源
相似解决方案