附加对数组的新引用?

时间:2016-07-17 23:05:03

标签: java arrays

我有一个数组接受3 parametersEmployees(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()方法时,Employee编号应更改为字符串数组中的数字:

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;    
 }

2 个答案:

答案 0 :(得分:0)

您正在assignEmployee方法中创建一个全新的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;    
 }

答案 1 :(得分:0)

而不是

list[i] = new Employee(number[i]);

您需要使用

list[i].setEmployeeNumber(number[i]);

使用您的代码,您可以创建一个只有员工编号的新Employee对象。 使用修改后的代码,您可以使用现有员工并设置员工编号。