gpt4 book ai didi

java - 升序排序

转载 作者:行者123 更新时间:2023-11-30 04:13:33 25 4
gpt4 key购买 nike

嗯,我的 java 项目即将完成,但还有一个部分需要处理,即排序。我必须根据员工的 ID 号按升序排序。我不确定使用哪种类型以及在哪里实现它。为它创建一个新类?让我向您展示我的测试代码。我的教员/兼职//职员/教育类(class)都在工作。这些类的代码都显示在 TestEmployee 中。因此,对于如何对我的 ID 号进行排序的任何帮助/提示,​​我们将不胜感激。

import java.util.Calendar;
import java.util.Scanner;

public class TestEmployee implements EmployeeInfo{


public static void main(String[] args) throws CloneNotSupportedException {

/**
* Part A: output all employee from Staff, Faculty, and partime.
*/

double sum=0,total=0;

Employee[]emp = new Employee[9];
Calendar staffBirthDate = Calendar.getInstance();
Calendar facultyBirthDate = Calendar.getInstance();
Calendar partimeBirthDate = Calendar.getInstance();

staffBirthDate.set(1959, 2, 23);// Scott Chan
emp[0] = new Staff("Chan, Scott", 123, 'M', staffBirthDate, 35.00);

staffBirthDate = Calendar.getInstance();

staffBirthDate.set(1964, 7, 12);// Brian Salinas
emp[1] = new Staff("Salinas, Brian", 456, 'F', staffBirthDate, 30.00);

staffBirthDate = Calendar.getInstance();

staffBirthDate.set(1970, 6, 2);// Allen Weir
emp[2] = new Staff("Weir, Allen", 789, 'M', staffBirthDate, 22.00);

facultyBirthDate = Calendar.getInstance();

facultyBirthDate.set(1962, 4, 27);
emp[3] = new Faculty("Im, Lee", 243, 'F', facultyBirthDate, "Full", "PH.D", "Engineering", "3");

facultyBirthDate = Calendar.getInstance();

facultyBirthDate.set(1975, 3, 14);//
emp[4] = new Faculty("Bui, Thung", 791, 'F', facultyBirthDate, "Associate", "PH.D", "English", "1");

facultyBirthDate = Calendar.getInstance();


facultyBirthDate.set(1980, 5, 22);//
emp[5] = new Faculty("Monreno, Maria", 623, 'F', facultyBirthDate, "Assistant", "MS", "Physical Education", "0");

partimeBirthDate = Calendar.getInstance();

partimeBirthDate.set(1977, 8, 10);
emp[6] = new Partime("Lee, Chesong", 455, 'F', partimeBirthDate, 20, 35.00);

partimeBirthDate = Calendar.getInstance();


partimeBirthDate.set(1987, 9, 15);
emp[7] = new Partime("Garcia, Frank", 678, 'M', partimeBirthDate, 25, 30.00);

partimeBirthDate = Calendar.getInstance();

partimeBirthDate.set(1980, 8, 22);//
emp[8] = new Partime("Alquilo, Roscoe", 945, 'M', partimeBirthDate, 30, 20.00);

for(int i = 0; i<emp.length;i++)
{
if(emp[i] instanceof Staff)
{
System.out.println("\n"+emp[i]);
}//end of if statement
if(emp[i] instanceof Faculty)
{
System.out.println("\n"+emp[i]);
}//end of if statement

}// end of for loop

for(int i = 0; i<emp.length; i++)
{
sum = ((Employee) emp[i]).monthlyEarning()+sum;
}
System.out.println("\nTotal monthly salary for all Employees");

System.out.println("$"+sum);
//c
System.out.println("\nTotal monthly salary for all faculuty");
for(int i = 0; i<emp.length;i++)
{
if(emp[i] instanceof Faculty)
{
total = ((Employee) emp[i]).monthlyEarning()+total;
}
}
System.out.println("$"+total);
// Duplicate a faculty object. test the duplication
Faculty f1 = (Faculty)emp[4];
Faculty f2 = (Faculty)f1.clone();
Education dupl = new Education("PH.D",
"Doctor", "4");
f2.setEducation(dupl);
System.out.println("\nD Duplicate a Faculty Object"
+"\n"+f2.toString());

// Verify two staff objects are the same

System.out.println("\nE.Verify two staff objects ");
Staff s1 = (Staff)emp[6];
Staff s2 = (Staff)s1.clone();
staffBirthDate = Calendar.getInstance();

Staff s3 = new Staff("Danger, Norman", 456, 'M', staffBirthDate, 25.00);
if(s1.getBirthdate()==s2.getBirthdate())
{
System.out.print("\nThe two staff objects " +
" birthdays"+ " are the same "
+"therefore "+true+"\n");
}
if(s3.getBirthdate()==s1.getBirthdate())
{
System.out.print(true);
}

// Sort employees by ascending employee ID
System.out.println("\nSort employees by ID");


{
System.out.println("\n"+emp[i]);
}
}
}

最佳答案

您通常不会实现排序。您可以使用Arrays.sort(根据您的情况)或Collections.sort。您所要做的就是提供一个定义排序的Comparator

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

关于java - 升序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18994569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com