import java.util.LinkedList;
import java.util.List;
public class StudentList2 {
public static void main(String[] args) {
StudentList2 st2 = new StudentList2();
//创建LinkedList泛型数组
List<Student1> llist = new LinkedList<Student1>();
// 创建6个学生对象并初始化
Student1 s1 = new Student1("张三", "男", 18);
Student1 s2 = new Student1("李四", "女", 19);
Student1 s3 = new Student1("王五", "男", 20);
Student1 s4 = new Student1("陆六", "女", 21);
Student1 s5 = new Student1("二狗子", "男", 22);
Student1 s6 = new Student1("刘元", "男", 23);
llist.add(s1);
llist.add(s2);
llist.add(s3);
llist.add(s4);
llist.add(s5);
// 插入第6个学生至第二个位置,删除最后一位学生
llist.add(1, s6);
llist.remove(llist.size()-1);
// 遍历打印每个学生对象的自我介绍方法
for (Student1 student : llist) {
student.printer();
}
}
}
/**
* 学生类
* @author Administrator
*
*/
class Student1{
// 姓名,性别,年龄
private String name;
private String gender;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 无参构造方法
public Student1(){}
// 有参构造方法
public Student1(String name,String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
// 自我介绍方法
public void printer(){
System.out.println("我是" + name + ",性别" + gender + ",今年" + age + "岁");
}
}