无法打印集合中的对象
建立了两个类,第一个类创建People对象,第二个类创建集合对象并打印,但是打印不成功,问题出在哪里呢?请老司机看看。(1)创建People对象:
public class People {
private String name;
private int cardID;
public People(String name, int cardID){
this.name=name;
this.cardID=cardID;
}
public String method1() {
return name;
}
public int method2() {
return cardID;
}
}
(2)建立List集合并打印
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
public class Jianliyigejihe {
public static void main(String[] args) {
List <People>list1=new LinkedList<People>();
list1.add(new People("阿一",2343));
list1.add(new People("阿二",4444));
list1.add(new People("阿三",5555));
System.out.println("集合中的元素是:");
Iterator<People> itt=list1.iterator();
while(itt.hasNext());
{
People d1=itt.next();
System.out.println(d1.method1()+" "+d1.method2());
}
}
}