Packagecn.itcast_02;ImportJava.util.ArrayList;ImportJava.util.Collection;/* * Exercise: Store 5 student objects in a collection and traverse the student objects. * * Analysis: * A: Create student class * B: Create a Collection Object * C: Create a Student Object * D: Add a student to the set * E: Transfer the set to the group * F: Iterate through the array */ Public class studentdemo { Public Static void Main(string[] args) {//Create collection ObjectCollection C =NewArrayList ();//Create student ObjectsStudent S1 =NewStudent ("Brigitte", -); Student s2 =NewStudent ("The Wind", -); Student s3 =NewStudent ("Make the fox Rush", -); Student S4 =NewStudent ("Wu Xin", -); Student S5 =NewStudent ("Lynn Song", A);//Add students to the collectionC.add (S1); C.add (S2); C.add (S3); C.add (S4); C.add (S5);//Turn the collection into a groupobject[] Objs = C.toarray ();//Traversal array for(intx =0; x < objs.length; X + +) {//System.out.println (objs[x]);Student s = (Student) objs[x]; System.out.println (S.getname () +"---"+ s.getage ()); } }}
Packagecn.itcast_02; Public class Student { //Member variables PrivateString name;Private intAge//Construction method Public Student() {Super(); } Public Student(String name,intAge) {Super(); This. name = name; This. Age = Age; }//Member Method //GetXxx ()/setxxx () PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Collection Collection Detailed 2