Collection Framework (collection store custom objects and traverse cases)

Source: Internet
Author: User

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0010.gif "alt=" J_0010.gif "/> first create a student class

Package Cn.itcast_03;public class Student {//member variable private String name;private int age;//constructor method public Student () {super ();} Public Student (String name, int.) {super (); this.name = Name;this.age = age;} Member method//GetXxx ()/setxxx () public String getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic String toString () {return "Student [name=" + name + ", age=" + Age + "]";}}

Re-write the test class

package cn.itcast_03;import java.util.arraylist;import java.util.collection;import  java.util.iterator;/* *  Exercise: Store 5 student objects in a collection and traverse through the student objects. Iterate with iterators.  *  *  Note:  * a: Your class name is not the same as the class name in the API that we are learning to use.  * b: When copying code, it is easy to import the package that the class is in, which is prone to problems that cannot be understood.  */public class iteratortest {public static void main (String[] args)  {//  Create a Collection Object Collection c = new arraylist ();//  Create a Student object student s1 =  new student ("Brigitte",  27); Student s2 = new student ("Wind Qing",  30); Student s3 = new student ("Make Fox Chong",  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);//  Traverse Iterator it = c.iterator ();while  (It.hasnext ())  {//  system.out.println (It.next ()); student s =  (Student)  it.next (); 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.