关键字:学习笔记,ArrayList
ArrayList中的indexOf、contains、remove的等方法实际需要equals方法的支持。其中contains方法调用的是indexOf方法:
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
在未覆写equals方法的情况下默认调用的是Object类的equals方法,判断的是内存地址:this == obj。所以在需要的情况下应该覆写equals方法。下面做一个比较:
未覆写情况下:
package com.java.demo;
import java.util.ArrayList;
import java.util.List;
class Member{
private String name;
private Integer age;
private Double salary;
public Member() {}
public Member(String name, Integer age, Double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Member [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
public class TestDemo {
public static void main(String[] args) throws Exception{
Member p1 = new Member("滚滚", 3, 0.001);
Member p2 = new Member("财财", 5, 12.51);
Member p3 = new Member("多多", 28, 12000.512);
List all = new ArrayList();
all.add(p1);
all.add(p2);
all.add(p3);
// all.remove(new Member("滚滚", 3, 0.001)); 不能删除
// all.remove(p1); //可以删除
System.out.println(all.contains(new Member("滚滚", 3, 0.001))); //false
System.out.println(all.contains(p1)); //true
System.out.println(all.indexOf(p1)); //0
System.out.println(all.indexOf(new Member("滚滚", 3, 0.001))); //-1
for (Integer i = 0; i < all.size(); i++) {
System.out.println(all.get(i));
}
//Member [name=滚滚, age=3, salary=0.001]
Member [name=财财, age=5, salary=12.51]
Member [name=多多, age=28, salary=12000.512]
}
}
覆写equals后:
package com.java.demo;
import java.util.ArrayList;
import java.util.List;
class Member{
private String name;
private Integer age;
private Double salary;
public Member() {}
public Member(String name, Integer age, Double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public boolean equals(Object obj) {//覆写equals()方法
if (this == obj) {
return true;
}
if(!(obj instanceof Member)||obj == null) {
return false;
}
Member temp = (Member)obj;//向下转型
return this.name.equals(temp.name)&&this.age.equals(temp.age)&&this.salary.equals(temp.salary);
}
@Override
public String toString() {
return "Member [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
public class TestDemo {
public static void main(String[] args) throws Exception{
Member p1 = new Member("滚滚", 3, 0.001);
Member p2 = new Member("财财", 5, 12.51);
Member p3 = new Member("多多", 28, 12000.512);
ArrayList all = new ArrayList();
all.add(p1);
all.add(p2);
all.add(p3);
// all.remove(new Member("滚滚", 3, 0.001)); //可以删除
// all.remove(p1); //可以删除
System.out.println(all.contains(new Member("滚滚", 3, 0.001)));
System.out.println(all.contains(p1));
System.out.println(all.indexOf(p1));
System.out.println(all.indexOf(new Member("滚滚", 3, 0.001)));
for (Integer i = 0; i < all.size(); i++) {
System.out.println(all.get(i));
}
}
}
执行结果:
true
true
0
0
Member [name=滚滚, age=3, salary=0.001]
Member [name=财财, age=5, salary=12.51]
Member [name=多多, age=28, salary=12000.512]