【发布时间】:2014-03-18 21:02:30
【问题描述】:
我知道有人问过这个问题,但我觉得我的问题是个特例。我还是没搞清楚。
因此,如果员工对象在部门信息系统或帐户中,我需要按他们的年龄对他们进行排序。
但是,我无法获取代码来仅对部门信息系统或会计部门中的员工进行排序,我将如何指定仅按年龄对它们进行排序并打印出其余部分?
以下是所有相关代码:
员工等级:
public class Employee {
private String name;
private int age;
private String department;
public String getDept(){
return department;
}//end dept
public void setDept(String dept){
this.department = dept;
}//end
public String getName(){
return name;
}//end name
public void setName(String n){
this.name = n;
}//end
public int getAge(){
return age;
}//end age
public void setAge(int a){
this.age = a;
}//end
public String toString(){
return name + " " + age + " " + department;
}//end to string
public Employee (String n,int age,String dept){
this.name = n;
this.age = age;
this.department = dept;
}//end employee
}//end class
部门类:
import java.util.*;
public class Department implements Comparator<Employee> {
@Override
public int compare(Employee o1, Employee o2){
return o1.getAge() - o2.getAge();
}//end method
}//end departmen
主方法类:
import java.util.*;
public class Company {
public static void main(String [] args){
PrimeAgeChecker p = new PrimeAgeChecker();
ArrayList<Employee> test = new ArrayList<Employee>();
test.add(new Employee("Counting Guru",55,"Accounting"));
test.add(new Employee("Counting Pro",45,"Accounting"));
test.add(new Employee("Counting Savvy",40,"Accounting"));
test.add(new Employee("Counting Novice",25,"Accounting"));
test.add(new Employee("Sales Guru",50,"Marketing"));
test.add(new Employee("Sales Pro",48,"Marketing"));
test.add(new Employee("Sales Savvy",38,"Marketing"));
test.add(new Employee("Hiring Guru",58,"Human Resrouces"));
test.add(new Employee("Hiring Pro",47,"Human Resrouces"));
test.add(new Employee("Hacking Pro",47,"Information Systems"));
test.add(new Employee("Hacking Guru",51,"Information Systems"));
test.add(new Employee("Hacking Savvy",38,"Information Systems"));
test.add(new Employee("Hacking Novice",23,"Information Systems"));
for(Employee i: test){
if(i.getDept().equals("Information Systems") || i.getDept().equals("Accounting")){
Collections.sort(test, new Department());
System.out.println(i + " " + p.isPrime(i));
}//end
else{
System.out.println(i + " " + p.isPrime(i));
}
}//end main
}//end company
【问题讨论】:
-
您能否为一小组输入显示您预期的排序输出?我能理解你在说什么,但想确认一下。
-
当然,看看主方法类:我希望它对 Employee 对象有参数(String,integer,“Accounting”)的地方进行排序,并对有参数的 Employee 对象(String,整数,“信息系统”)。我不想对最后一个参数为“营销”或“人力资源”的其他对象进行排序。
-
否 - 我要的是排序输出的示例。请不要以文字的标准。