import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
class Student
{
String name ;
int age;
public Student(String name , int age){
this.name = name;
this.age = age;
}
}
class Demo
{
public static void main(String[] args)
{
Collecion c = new ArrayList();
Student stu = new Student("小米",4);
Student stu1 = new Student("诺基亚",10);
Student stu2 = new Student ("HTC",6);
c.add(stu);
c.add(stu1);
c.add(stu2);
System.out.println(c.size());
//遍历得出每一个元素.
Iterator it = c.iterator();
while(it.hasNext()){
Student s = (Student)it.next;
//toString方法获取每一个学生对象.
System.out.println(s.name+","+s.age);
}
}
}
|
|