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)