The collection object cannot display the output of some undefined values in Java, ArrayList and collection

When I try to print a collection object, it prints employee @ 122392iie92 Why print this instead of the details of the employee list?

My code:

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

}

Solution

It prints using the default toString method of the object class If you want to display values, you need to override toString. In the Employee class It currently displays the class name and hash code

Write such a toString method in employee:

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

Move the new statement within the loop, otherwise you will add and update the same object again and again

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

  }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>