一下两段代码,实例一和实例二。传入的都是对象,为什么打印出来的结果不一样呢?
- /*
- 实例一:
- */
- import java.util.*;
- class Person
- {
- String name;
- int age;
- Person(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public void get()
- {
- System.out.println(name+"..."+age);
- }
- }
- class IteratorDemo
- {
- public static void main(String[] args)
- {
- Person p1 = new Person("zhang san",30);
- Person p2 = new Person("li si",40);
- Person p3 = new Person("wang wu",50);
- Person p4 = new Person("zhou liu",60);
- ArrayList a1 = new ArrayList();
- a1.add(p1);
- a1.add(p2);
- a1.add(p3);
- a1.add(p4);
-
- for (Iterator it =a1.iterator();it.hasNext(); )
- {
- System.out.println(it.next());
- }
- }
- }
复制代码- /*
- 实例二:
- */
- import java.util.*;
- class CollectionDemo2
- {
- public static void main(String[] args)
- {
- ArrayList a2 = new ArrayList();
- a2.add("java5");
- a2.add("java6");
- a2.add("java7");
- a2.add("java8");
- a2.add("java9");
- for (Iterator it = a2.iterator();it.hasNext(); )
- {
- System.out.println(it.next());
- }
- }
- }
复制代码 |