以下程序的输出不是我所期望的。这个程序有什么问题?请建议。
[Hello113,Hello380,Hello293,Hello290,Hello246,Hello456,Hello797,Hello888,Hello981]
编辑:我只想这样做。不使用 collections.sort()。
public class Sample {
public static void main(String[] args) {
Random random = new Random(10);
List<Employee> employees = new ArrayList<Employee>();
for (int i=0;i<10;i++)
{
Employee e = new Employee(i,"Hello"+random.nextInt(1000));
employees.add(e);
}
System.out.println(employees);
}
}
class Employee implements Comparable<Employee>
{
private int empNo;
private String empName;
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
Employee(int empNo,String empName)
{
this.empName=empName;
this.empNo=empNo;
}
@Override
public String toString() {
return empName+" \n";
}
@Override
public int compareTo(Employee o) {
return this.getEmpName().compareTo(o.getEmpName());
}
}