1.student java
package ArrayList; public class Student { private String name; private int age; private String sex; public Student() { } public Student(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
2.Student_demo
package ArrayList; import java.util.ArrayList; public class student_demo { public static void main(String[] args) { Student one = new Student("Alex", 18, "male"); Student two = new Student("Hull", 20, "male"); Student three = new Student("Linda", 21, "female"); Student four = new Student("Miranda", 18, "female"); ArrayList<Student> list = new ArrayList<>(10); list.add(one); list.add(two); list.add(three); list.add(four); for(int i=0;i<list.size();i++){ Student stu=list.get(i); System.out.println("student: "+stu.getName()+" age: "+stu.getAge()+" sex: "+stu.getSex()); } } }
ret://
student: Alex age: 18 sex: male
student: Hull age: 20 sex: male
student: Linda age: 21 sex: female
student: Miranda age: 18 sex: female