员工等级:
public class Employee {
int empid;
String name;
int age;
public Employee(int empid,String name,int age)
{
this.empid=empid;
this.name=name;
this.age=age;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
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;
}
}
比较器类:
public class Employee_comparator implements Comparator<Employee> {
@Override
public int compare(Employee object1, Employee object2) {
return object1.getname().compareTo(object2.getname());
}
}
主类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Employee_Main {
/**
* @param args
*/
public static void main(String[] args) {
List<Employee> list=new ArrayList<Employee>();
list.add(new Employee(33186,"varun",23));
list.add(new Employee(33187,"deepak",23));
list.add(new Employee(33188,"apple",23));
list.add(new Employee(33189,"rohan",23));
Collections.sort(list,new Employee_comparator());
for(int i=0;i<list.size();i++) {
System.out.print("age:"+list.get(i).getAge());
System.out.print("empid:"+list.get(i).getEmpid());
System.out.println("name:"+list.get(i).getname());
}
Iterator<Employee> itr=list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
当我尝试使用 get 方法打印时,它工作正常。但是,当我尝试使用 打印列表的元素时Iterator
,它会给出以下输出:
new_.Employee@28122812new_.Employee@280d280dnew_.Employee@28172817new_.Employee@28082808