Java Collection Collection Detailed 2

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.