【问题标题】:How to get name from a list of employees如何从员工列表中获取姓名
【发布时间】:2019-09-30 09:22:52
【问题描述】:

我正在尝试从员工列表中打印员工的“姓名”。下面是我的 pojo 课。

import java.time.LocalDate;

public class Employee {

    private String name;
    private String empID;
    private Designation designation;
    private LocalDate dateOfJoining;
    private int monthlySalary;

    public Employee(String name, String empID, Designation designation, LocalDate dateOfJoining, int monthlySalary) {
        super();
        this.name = name;
        this.empID = empID;
        this.designation = designation;
        this.dateOfJoining = dateOfJoining;
        this.monthlySalary = monthlySalary;
    }

    public Employee() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmpID() {
        return empID;
    }

    public void setEmpID(String empID) {
        this.empID = empID;
    }

    public Designation getDesignation() {
        return designation;
    }

    public void setDesignation(Designation designation) {
        this.designation = designation;
    }

    public LocalDate getDOJ() {
        return dateOfJoining;
    }

    public void setDOJ(LocalDate dOJ) {
        dateOfJoining = dOJ;
    }

    public int getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(int monthlySalary) {
        this.monthlySalary = monthlySalary;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((dateOfJoining == null) ? 0 : dateOfJoining.hashCode());
        result = prime * result + ((designation == null) ? 0 : designation.hashCode());
        result = prime * result + ((empID == null) ? 0 : empID.hashCode());
        result = prime * result + monthlySalary;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (dateOfJoining == null) {
            if (other.dateOfJoining != null)
                return false;
        } else if (!dateOfJoining.equals(other.dateOfJoining))
            return false;
        if (designation == null) {
            if (other.designation != null)
                return false;
        } else if (!designation.equals(other.designation))
            return false;
        if (empID == null) {
            if (other.empID != null)
                return false;
        } else if (!empID.equals(other.empID))
            return false;
        if (monthlySalary != other.monthlySalary)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", empID=" + empID + ", designation=" + designation + ", DOJ=" + dateOfJoining
                + ", monthlySalary=" + monthlySalary + "]";
    }

}

现在,当我尝试打印“名称”时,我得到了空值。 请看下图,

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Employecomparable {

    public static void main(String[] args) {
        JoiningDate jd = new JoiningDate();
        Employee emp = new Employee();
        List<Employee> listofemployee = new ArrayList<>();

        listofemployee.add(new Employee("abc", "12345", Designation.ASE,jd.date1 , 20000));
        listofemployee.add(new Employee("abcd", "24680", Designation.SE, jd.date2, 30000));
        listofemployee.add(new Employee("abcde", "13570", Designation.SSE,jd.date3, 40000));
        listofemployee.add(new Employee("abcdef", "13690", Designation.TL, jd.date4, 60000));
        listofemployee.add(new Employee("xyz", "10909", Designation.AM, jd.date5, 800000));
        listofemployee.add(new Employee("koool", "89076", Designation.M, jd.date6, 2000));
        System.out.println("The name of employee is "+emp.getName());

我得到的输出是“null”。 在我得到的控制台中,员工的姓名为空。 我如何打印名称? 请帮忙!

【问题讨论】:

  • 您正在将 名员工添加到列表中。你想打印谁的名字?
  • 假设我想打印第一个员工的姓名?我该怎么做?
  • 您没有在您创建的 emp 对象中设置任何属性

标签: java arrays list


【解决方案1】:

要打印特定员工的姓名,请更新 emp 对象。

emp = listofemployee.get(indexWithinTheList);
System.out.println("The name of employee is "+emp.getName());

示例

emp = listofemployee.get(0);
System.out.println("The name of employee is "+emp.getName());

Output : The name of employee is abc

【讨论】:

    【解决方案2】:

    您可能希望遍历您的员工,然后打印姓名。你得到null 的原因是你使用默认构造函数创建了Employee 对象。这意味着作为字符串对象的名称将采用您所看到的默认空值。

    如果要打印员工姓名,可以执行以下操作:

    for (Employee employee: listofemployee) {
        System.out.println("The name of employee is " + employee.getName());
    }
    

    由于您使用的是List 接口,您可以通过它的索引访问单个对象。例如。获取第一个员工姓名:

    System.out.println("The name of employee is " + listofemployee.get(0).getName());
    

    【讨论】:

      【解决方案3】:

      您遇到的错误是因为您创建了如下所示的 Employee 对象。 Employee emp = new Employee(); 之后,您将使用新员工填充列表。但是您没有为变量 emp 设置任何值。所以 emp 中的 getName 将返回 null。如果你想从 emp.您首先需要使用emp.SetName("Any name"); 设置名称不,您将使用emp.getName(); 获取值

      【讨论】:

        【解决方案4】:

        1 - 创建 Employee 对象,如下所示:-

        Employee emp =new Employee("xyz", "10909", Designation.AM, jd.date5, 800000);

        2 - 然后存储到列表中。

        3 - 之后根据您的要求迭代列表并打印结果。

        public static void main(String[] args) {
            JoiningDate jd = new JoiningDate();
            Employee emp = new Employee();
            List<Employee> listofemployee = new ArrayList<>();
            emp = new Employee("abc", "12345", Designation.ASE,jd.date1 , 20000);
            listofemployee.add(emp);
            emp =new Employee("abcd", "24680", Designation.SE, jd.date2, 30000);
            listofemployee.add(emp);
            emp =new Employee("abcde", "13570", Designation.SSE,jd.date3, 40000);
            listofemployee.add(emp);
            emp = new Employee("abcdef", "13690", Designation.TL, jd.date4, 60000);
            listofemployee.add(emp);
            emp =new Employee("koool", "89076", Designation.M, jd.date6, 2000);
            listofemployee.add(emp);
            emp =new Employee("xyz", "10909", Designation.AM, jd.date5, 800000);
            listofemployee.add(emp);
        
            System.out.println("The name of employee is "+emp.getName());
        
        
            Iterator<Employee> itr = listofemployee.iterator(); 
        
            // checking the next element availabilty 
            while (itr.hasNext()) 
            { 
               System.out.println("name " + (itr.next()).getName());
            }
        }
        

        【讨论】:

        • @michalk 完成。谢谢。它只是从我的脑海中溜走。
        猜你喜欢
        • 2017-01-07
        • 2019-02-13
        • 1970-01-01
        • 2020-04-04
        • 1970-01-01
        • 2022-12-12
        • 1970-01-01
        • 1970-01-01
        • 2022-11-18
        相关资源
        最近更新 更多