Collection Framework (collection store custom objects and traverse cases)

Source: Internet
Author: User

 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)

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.