有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

集合对象无法显示在java、Arraylist、Collection中显示某些未定义值的输出

当我尝试打印集合对象时,它会打印Employee@122392Iie92。为什么要打印这个而不是员工名单的详细信息

我的代码:

    public class Employee {

    private String name;
    private String designation;
    private int employeeId;
    private int salary;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public int getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }

}

import java.util.ArrayList;

import java.util.Scanner;


public class EmployeeManagement {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ArrayList <Employee> lst = new ArrayList <Employee> ();

        System.out.println("Enter the number of employees : ");

        int num = sc.nextInt();

        EmployeeManagement emp = new EmployeeManagement();

        emp.addEmployeeName( num, lst);


    }

    public void addEmployeeName(int num,ArrayList<Employee> lst) {

        Employee em = new Employee();

        for(int i =0; i<num ; i++)
        {
            System.out.println("Enter the employee id : ");
            em.setEmployeeId(sc.nextInt());

            System.out.println("Enter the name of employee : ");
            em.setName(sc.next());

            System.out.println("Enter the designation of employee : ");
            em.setDesignation(sc.next());

            System.out.println("Enter the Salary of employees : ");
            em.setSalary(sc.nextInt());

            lst.add(em);        
        }

        System.out.println(lst);
    }

}

共 (1) 个答案

  1. # 1 楼答案

    它使用Object类的默认toString方法进行打印。如果要显示值,需要重写Employee类中的toString。它当前显示类名和哈希代码

    在Employee中编写如下的toString方法:

     @Override
     public String toString(){
           SubString sb = new SubString();
           sb.append("Name :- ")append(name).append(id);  //all relevant fields
           return sb.toString();
     }
    

    将新语句移到循环中,否则您将一次又一次地添加和更新同一对象

     public void addEmployeeName(int num,ArrayList<Employee> lst) {
    
    
    
        for(int i =0; i<num ; i++)
        {
             Employee em = new Employee();
            System.out.println("Enter the employee id : ");
            em.setEmployeeId(sc.nextInt());
    
            System.out.println("Enter the name of employee : ");
            em.setName(sc.next());
    
            System.out.println("Enter the designation of employee : ");
            em.setDesignation(sc.next());
    
            System.out.println("Enter the Salary of employees : ");
            em.setSalary(sc.nextInt());
    
            lst.add(em);        
        }
    
        System.out.println(lst);
    }
    
      }