java 运行完iterator.hasnext() foreach不运行了
packagereread;importjava.util.*;classPerson{privateStringname,type;publicPerson(){}pu...
package reread;import java.util.*;class Person{ private String name,type; public Person() { } public Person(String name, String type) { this.name = name; this.type = type; } @Override public String toString() { return "Person [name=" + name + ", type=" + type + "]"; } //需要重写Hashcode equals @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (type == null) { if (other.type != null) return false; } else if (!type.equals(other.type)) return false; return true; } }public class ArrayListDemo { public static void main(String[] args) { // TODO Auto-generated method stub List<Person> list=new ArrayList<>(); list.add(new Person("孙悟空","神仙")); list.add(new Person("唐僧","人类")); list.add(new Person("猪八戒","神仙")); list.add(new Person("沙僧","人类")); list.add(new Person("白骨精","妖怪")); list.set(0, new Person("Monkey","神仙")); System.out.println(list.get(1)); System.out.println("初始------------"); for(Person p:list){ System.out.println(p); } System.out.println("Iterator遍历-----------"); Iterator<Person> it=list.iterator(); while(it.hasNext()){ Person per=it.next(); System.out.println(per); if(it.equals(new Person("白骨精","妖怪"))); it.remove(); } System.out.println("最终遍历------------"); for(Person p:list){ System.out.println(p); } }}最后的最终遍历却没有显示下文怎么解决? 为什么别人的都显示了
展开
展开全部
import java.util.*;
class Person {
private String name, type;
public Person() {
}
public Person(String name, String type) {
this.name = name;
this.type = type;
}
@Override
public String toString() {
return "Person [name=" + name + ", type=" + type + "]";
}
// 需要重写Hashcode equals
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
}
public class ArrayListDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Person> list = new ArrayList<>();
list.add(new Person("孙悟空", "神仙"));
list.add(new Person("唐僧", "人类"));
list.add(new Person("猪八戒", "神仙"));
list.add(new Person("沙僧", "人类"));
list.add(new Person("白骨精", "妖怪"));
list.set(0, new Person("Monkey", "神仙"));
System.out.println(list.get(1));
System.out.println("初始------------");
for (Person p : list) {
System.out.println(p);
}
System.out.println("Iterator遍历-----------");
Iterator<Person> it = list.iterator();
while (it.hasNext()) {
Person per = it.next();
System.out.println(per);
if (per.equals(new Person("白骨精", "妖怪")))
it.remove();
}
System.out.println("最终遍历------------");
for (Person p : list) {
System.out.println(p);
}
}
}
两个问题,
第一个此物你的if后面多加了一个;导致最后的it.remove()和if没关系了,所以不显示的原因是你把list里面的所有元穗简素都remove掉了
其次,判断Person的时候用it去equals,这个永远森族液不会相等的,对象类型都不同
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询