}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
报错信息如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from Iterator to HTMLDocument.Iterator
The method sop(Object) in the type ConnectionDemo is not applicable for the arguments (void)
at collection.ConnectionDemo.method_get(ConnectionDemo.java:28)
at collection.ConnectionDemo.main(ConnectionDemo.java:15) 作者: 周玉龙 时间: 2012-7-21 12:28
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
* 练习:用集合存储自定义对象,并输出每个对象元素.并画内存图
*/
public class IteratorTest {
public static void main(String[] args) {
ArrayList<Student> c = new ArrayList<Student>();
Student s1 = new Student();
s1.setName("欧阳锋");
s1.setAge(88);
Student s2 = new Student();
s2.setName("郭靖");
s2.setAge(66);
Student s3 = new Student();
s3.setName("杨过");
s3.setAge(48);
c.add(s1);
c.add(s2);
c.add(s3);
//System.out.println(c);
Iterator<Student> it = c.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName()+"***"+s.getAge());
// System.out.println(((Student)it.next()).getName()+"***"+((Student)it.next()).getAge());//这种写法编译不不错,但是运行就挂了,而上面的将it.next()赋给Student对象则安全
}
}
}
---------------------------------------------------------华丽的分割线-------------------------------------------------------------------
/*
* 自定义对象
*/
public class Student {
private String name;
private int age;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}