【问题标题】:Collection object can't show output showing some undefined value in java, Arraylist, Collection集合对象无法显示在 java、Arraylist、Collection 中显示一些未定义值的输出
【发布时间】:2014-12-10 13:53:16
【问题描述】:

当我尝试打印集合对象时,它会打印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);
    }

}

【问题讨论】:

    标签: java arraylist collections


    【解决方案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);
    }
    
      }
    

    【讨论】:

    • @KINGBHAI 如果这对您有用,请通过单击 anwser 侧面的箭头来接受答案。因此,其他人将通过看到该答案获得帮助。欢迎使用 Stackoverflow
    • 嘿,但它为不同的员工显示相同的输出。
    • 输出:- 输入员工人数:2 输入员工编号:1 输入员工姓名:2 输入员工名称:3 输入员工薪水:4 输入员工编号: 5 输入员工姓名:6 输入员工名称:7 输入员工薪水:8 [6587, 6587]
    • 有人可以告诉我为什么不赞成这个答案吗?这样我以后可以避免同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2011-11-30
    相关资源
    最近更新 更多