哪位大神能解释能改正求解释
- public class User implements Cloneable{
- private String name;
- private int age;
-
- public User(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public boolean equals(Object obj) {
- if(this == obj) {
- return true;
- }
- if(!(obj instanceof User)) {
- return false;
- }
- User user = (User)obj;
- //if(this.name==user.name && this.age==user.age)
- if(this.name.equals(user.name)
- && this.age==user.age) {
- return true;
- }
- else {
- return false;
- }
- }
- public int hashCode() {
- return name.hashCode() + age;
- }
-
- public String toString() {
- return "{name:'" + name + "',age:" + age + "}";
- }
- public Object clone() {
- Object object = null;
- try {
- object = super.clone();
- } catch (CloneNotSupportedException e) {}
- return object;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- }
复制代码- Collection users = new ArrayList();
- users.add(new User("张三",28));
- users.add(new User("李四",25));
- users.add(new User("王五",31));
- Iterator itrUsers = users.iterator();
- while(itrUsers.hasNext()){
- System.out.println("aaaa");
- User user = (User)itrUsers.next();
- if("张三".equals(user.getName())){
- users.remove(user);
- //itrUsers.remove();
- } else {
- System.out.println(user);
- }
- }
复制代码 |