自定义对象类: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 void setName(String name){ this.name = name; } public String getName(){ return this.name; } public void setAge(int age){ this.age = age; } public int getAge(){ return this.age; } }
----------------------------------------------------
import java.util.ArrayList; public class Demo { public static void main(String[] args) { //创建Student类型的集合对象ab ArrayList<Student> ab = new ArrayList<Student>(); //创建Student对象 Student s1 = new Student("Ven",100); Student s2 = new Student("Bob",66); Student s3 = new Student("Mary",33); //将Student对象追加到集合容器对象ab中 ab.add(s1); ab.add(s3); ab.add(s2); ????????//遍历集合对象ab,并输出对象属性值 for(int i=0;i<ab.size();i++){ Student s = ab.get(i); System.out.println(s.getName()+","+s.getAge()); } } }