ls.add(new Employee(25,"XYZ"));
Iterator it=(Iterator) ls.iterator();
while(it.hasNext())
{
System.out.println(" "+it);
}
这给出了垃圾值
答案 0 :(得分:0)
如果您在id
课程中定义了name
和Employee
字段,则应对代码进行更改后应解决此问题。
List<Employee> ls = new ArrayList<MergeSort.Employee>();
ls.add(new Employee(25, "XYZ"));
Iterator<Employee> it = ls.iterator();
while(it.hasNext()){
Employee employee = it.next();
System.out.println("Id: " + employee.getId() + " Name: " + employee.getName());
}
你应该使用迭代器的next()
方法从列表中获取元素。