List集合存储学生对象并遍历。
package List;
import java.util.ArrayList;
import java.util.List;
import com.heima.bean.Student;
public class Demo02 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("马王", 23));
list.add(new Student("马老大", 23));
list.add(new Student("马老二", 23));
list.add(new Student("马老三", 23));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));// 通过索引获取每一个元素
Student stu = (Student) list.get(i);
System.out.println("年龄:" + stu.getAge() + "姓名:" + stu.getName());
}
}
}
添加微信进交流群: opiopi293