package cn.itcast_02;import java.util.arraylist;import java.util.collection;/* * Exercise: Store 5 student objects in a collection and traverse through the student objects. * * Analysis: * a: Creating a Student Class * b: Creating a Collection Object * c: Creating a Student object * d: Add a student to the collection * e: sets the set to a group of * f: iterates through the array */public class studentdemo { public static void main (String[] args) { // Create collection Object collection c = new arraylist (); // Create student Object student s1 = new student ("Brigitte", 27) student s2 = new student ("Wind Qing", 30); student s3 = new student ("Linghutao", 33); student s4 = new student ("Wu Xin", 25); student s5 = new student ("Lynn Qu", 22); // add students to the collection c.add (S1); c.add (S2); c.add (S3); c.Add (S4); c.add (S5); // sets the set to the array object[] objs = c.toarray ( ); // traversal array for (int x = 0; x < objs.length; x++) { // system.out.println (objs[x]); student s = (Student) objs[x]; system.out.println (S.getname () + "---" + S.getage ()); } }}
This article from "GD" blog, reproduced please contact the author!
Collection Framework (collection store custom objects and traverse cases)