gpt4 book ai didi

java - 将新引用附加到数组?

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:37 25 4
gpt4 key购买 nike

我有一个接受 3 个参数的数组:Employees(firstNames, lastNames, idNumbers),它们是字符串数组。

String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};

当我调用打印方法时,我得到输出:

First name: Fred
Last name: Bond
Id number: R111111
Employee number: No employee number has been assigned yet!

First name: John
Last name: Kates
Id number: A222222
Employee number: No employee number has been assigned yet!

等等...

如您所见,尚未分配员工编号。但是,当我使用 assignEmployeeNum() 方法时,员工编号应更改为字符串数组中的编号:

String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};

但是我得到了这个输出:

+++After adding the employee number to the list+++

First name: null
Last name: null
Id number: null
Employee number: 1111

First name: null
Last name: null
Id number: null
Employee number: 2222

等等...

我对这个问题的最初尝试只是添加另一个专门针对员工编号的构造函数,但显然这不起作用。任何建议将不胜感激。

主要方法

public static void main(String[] args)
{
String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};

Employee[] list = new Employee[firstNames.length];
list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array
printEmployeeList(list); //print the list of employees
list = assignEmployeeNum(list,employeeNum); // assign the employee number to the employees

员工类别

public static class Employee{
private String firstName;
private String lastName;
private String idNumber;
private String employeeNumber;
private int employeeCount;

/**
* Constructor
* @param firstName first name
* @param lastName last name
* @param idNumber id number
*/
public Employee(String firstName, String lastName, String idNumber){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
employeeCount = 0;
}

public Employee(String EmployeeNum){
this.employeeNumber = EmployeeNum;
}
/**
* Accessors here
*/

public String getFirstName(){
return firstName;
}

public String getLastName(){
return lastName;
}

public String getIdNumber(){
return idNumber;
}

public String getEmployeeNumber(){
return employeeNumber;
}

// mutators here

/**
* @param firstName first name
*/
public void setFirstName(String firstName){
this.firstName = firstName;
}

/**
* @param lastName last name
*/
public void setLastName(String lastName){
this.lastName = lastName;
}

/**
* @param idNumber id number
*/
public void setIdNumber(String idNumber){
this.idNumber = idNumber;
}

/**
* @param employeeNumber employee number
*/
public void setEmployeeNumber(String employeeNumber){
this.employeeNumber = "";
}
@Override
public String toString(){
String result = "\nFirst name: " + getFirstName() + "\nLast name: " + getLastName()
+ "\nId number: " + getIdNumber() + "\nEmployee number: ";
if(getEmployeeNumber() == null){
return result + "No employee number has been assigned yet!\n";
}
return result + getEmployeeNumber() + "\n";
}

}

分配员工方法

public static Employee[] assignEmployeeNum(Employee[] list, String[] number){
for(int i = 0; i<list.length; i++){
list[i] = new Employee(number[i]);
}
return list;
}

最佳答案

您将在 allocateEmployee 方法中创建一个全新的 Employee 对象,该对象的每个字段都为 null 值,而不是使用数组中已存在的对象。解决方案:不要这样做。相反,采用数组中该位置保存的当前 Employee 对象,并在方法中简单地设置其员工 ID。

例如,

public static Employee[] assignEmployeeNum(Employee[] list, String[] number){
for(int i = 0; i<list.length; i++){
list[i].setEmployeeNumber(number[i]);
}
return list;
}

关于java - 将新引用附加到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38426742/

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