package list;
import java.util.ArrayList;
import java.util.Iterator;
import snippet.Person;
public class Array {
public static void main(String[] args) {
ArrayList a=new ArrayList();
a.add(new Person("lili",21));
a.add(new Person("qiqi",22));
a.add(new Person("wili",23));
System.out.println(a);
Iterator b=a.iterator();
while(b.hasNext())
{
// System.out.println(((Person) b.next()).getName()+((Person) b.next()).getAge());
Person p=(Person) b.next();
System.out.println(p.getName()+p.getAge());
// System.out.println(b.next());
}
}
}
如果把上面代码中的强转即Person p=(Person) b.next();换掉就打印的是哈希值,再往Person类中加入// public String toString()
// {
// return name+":"+age;
// }
就又打印出来了,
我想问这个toString和强转的作用是一样的吗? |