gpt4 book ai didi

java - ArrayList 中的 "Duplicate"条目?

转载 作者:行者123 更新时间:2023-12-01 22:24:53 24 4
gpt4 key购买 nike

我有一个类,它将用数组中预先创建的所有员工填充一个列表。我可以用员工填充 ArrayList,但唯一的问题是我得到一些“重复”条目,我使用引号,因为它们不完全相同,但他们可以共享相同的姓名或员工编号,但可能没有相同的雇用年份或工资等。

这是员工类别:

public class Employee {

public String EmployeeName;
public String EmployeeNumber;
public int hireyear;
public double WeeklyEarning;

public Employee()
{
EmployeeName = null;
EmployeeNumber = null;
hireyear = 0;
WeeklyEarning = 0;

}

public static final String[] Empnum = new String[] {
"0001-A", "0002-B","0003-C","0004-D","0002-A",
"0003-B","0004-C","0005-D","0011-A", "0012-B",
"0013-C","0014-D","0121-A", "0122-B","0123-C",
"0321-A", "0312-B","1234-D","4321-C","1122-D"};

public static final String[] Ename = new String[] {
"Josh", "Alex", "Paul", "Jimmy", "Josh", "Gordan", "Neil", "Bob",
"Shiv", "James", "Jay", "Chris", "Michael", "Andrew", "Stuart",
"Dave","Benjamin","Dan","Brian","Michelle"};

public String getEmployeeName()
{
return this.EmployeeName;
}

public String getEmployeeNumber()
{
return this.EmployeeNumber;
}

public int gethireyear()
{
return this.hireyear;
}

public double getWeeklyEarning()
{
return this.WeeklyEarning;
}

public String setEmployeeName(String EName)
{
return this.EmployeeName = EName;
}

public String setEmployeeNumber(String ENumber)
{
return this.EmployeeNumber = ENumber;
}

public int setEmployeehireyear(int Ehireyear)
{
return this.hireyear = Ehireyear;
}

public double setEmployeeweeklyearning(double Eweeklyearning)
{
return this.WeeklyEarning = Eweeklyearning;
}

public String toString(){
String data = "\n Employee Name : " + EmployeeName + " \n Employee Number: " + EmployeeNumber + " \n Hire Year : " + hireyear + "\n Weekly Earnings : " + WeeklyEarning;
return data;
}

public boolean equals(Object o){
if(this == null){
return false;
}
if(this == o){
return true;
}
if(!(o instanceof Employee)){
return false;
}

Employee temp = (Employee) o;
if(this.getEmployeeName().equals(temp.getEmployeeName())){
return true;

}
if(this.getEmployeeNumber().equals(temp.getEmployeeNumber())){
return true;

}
if(this.gethireyear() == temp.gethireyear()){
return true;
}
if(this.getWeeklyEarning() == temp.getWeeklyEarning()){
return true;
}
return false;

}

}

这是将填充列表的generateList方法:

public ArrayList<Employee> generateEmpList(){
empList = new ArrayList <Employee>();
Random empPicker = new Random();

for(int i = 0; i < 20; i++){
int id = empPicker.nextInt(20);
if(id < 12) // roll for production worker
{

//System.out.println("Adding Production Worker");
ProductionWorker temp = new ProductionWorker();
temp = temp.generateProductionWorker();
prodWorker = temp;
empList.add(prodWorker);

}
else //roll for Shift supervisor
{
//System.out.println("Adding Shift supervisor");
ShiftSupervisor supervisor = new ShiftSupervisor();
supervisor = supervisor.generateShiftSupervisor();
shiftWorker = supervisor;
empList.add(shiftWorker);
}
}
Iterator iterator = empList.iterator();
while (iterator.hasNext()) {
System.out.println("");
System.out.println(iterator.next());

}
return empList;
}

另外可能有用的是“generateProductionWorker()”和shiftSupervisor方法 - 为了保持简短,只有后期生产工作人员方法,因为它们基本上是相同的:

public ProductionWorker generateProductionWorker(){
Random rng = new Random();
int numberOfEmployeeNames = Ename.length;
ProductionWorker tempPworker = new ProductionWorker();

String employeeName = Ename[rng.nextInt(numberOfEmployeeNames)];
tempPworker.setEmployeeName(employeeName);

int numberOfEmployeeNumbers = Empnum.length;
String employeeNumber = Empnum[rng.nextInt(numberOfEmployeeNumbers)];
tempPworker.setEmployeeNumber(employeeNumber);

int yearHired = rng.nextInt(35) + 1980;
tempPworker.setEmployeehireyear(yearHired);

double weeklySalary = rng.nextInt((100) * 100);
tempPworker.setEmployeeweeklyearning(weeklySalary);

int hourlyRate = rng.nextInt(20) + 10;
tempPworker.setHourlyRate(hourlyRate);

return tempPworker;

}

我确信我错过了一些微不足道的东西,但你知道为什么当我有 20 个姓名和号码的列表时我会得到类似的条目吗?

例如:员工姓名 - 乔什empnum - 0000-A雇佣年份 - 1994工资 - 40,000

员工姓名 - 乔什empnum - 0000-A雇佣年份 - 1999工资 - 60,500

任何建议都会有所帮助,谢谢!

最佳答案

查看 Employee 类中的 equals 方法。如果它们的名字相同,则返回 true,这意味着它们是相等的。其他属性也是如此。您必须替换 if 语句。

关于java - ArrayList 中的 "Duplicate"条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990611/

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