【发布时间】:2017-06-03 08:32:14
【问题描述】:
我现在正在学习java。在编写使用Iterator 遍历ArrayList 的代码时,我必须在使用带有next() 函数的迭代器对象之前使用类名。有人可以帮我解决这个问题吗?
import java.util.*;
public class arraylistwithuserdefinedclass {
public static void main(String[] args) {
ArrayList<UserId> details=new ArrayList<UserId>();
UserId a= new UserId(22,"gurmeet");
UserId b= new UserId(24,"sukhmeet");
details.add(a);
details.add(b);
Iterator itr = details.iterator();
while(itr.hasNext()) {
UserId ui = (UserId) itr.next();
System.out.println(ui.age +" " + "" + ui.name) ;
}
}
}
class UserId {
int age;
String name;
UserId(int a, String b) {
age=a;
name=b;
}
}
【问题讨论】:
标签: java collections