思路
1、定义学生对象
2、创建List集合对象
3、创建学生类
4、把学生添加到集合
5、遍历集合
导包
import java.util.ArrayList; import java.util.List;
public static void main(String[] args) { //创建List集合对象 List附录list = new ArrayList (); //创建学生类 Student s1 = new Student("张三", 18); Student s2 = new Student("李四", 19); Student s3 = new Student("王五", 20); //把学生添加到集合 list.add(s1); list.add(s2); list.add(s3); //遍历 for (int i = 0; i < list.size(); i++){ Student s = list.get(i); System.out.println(s.getName()+","+s.getAge()); } }
Student类
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)