| 
 
| 使用增强for循环遍历集合,请大家指! 
 package cn.itheima;
 
 import java.util.ArrayList;
 import java.util.Collection;
 
 class Person{
 String name;
 int id;
 
 @Override
 public String toString() {
 // TODO Auto-generated method stub
 return this.name+this.id;
 }
 public Person(String name,int id) {
 super();
 this.name = name;
 this.id=id;
 }
 @Override
 public int hashCode() {
 final int prime = 31;
 int result = 1;
 result = prime * result + id;
 result = prime * result + ((name == null) ? 0 : name.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 (id != other.id)
 return false;
 if (name == null) {
 if (other.name != null)
 return false;
 } else if (!name.equals(other.name))
 return false;
 return true;
 }
 
 }
 
 public class Demo5 {
 
 /**
 * @param args
 */
 public static void main(String[] args) {
 ArrayList c=new ArrayList();
 c.add(new Person("李三",110));
 c.add(new Person("张三",120));
 c.add(new Person("王五",130));
 System.out.println(new Person("王五",130));
 Object[] o=c.toArray();
 for(Object p:c){
 Person p1=(Person)p;
 if(p1.id==120){
 System.out.println(p1);
 }
 
 }
 
 
 }
 
 }
 
 
 
 | 
 |