收集对象可以';t显示在java、Arraylist和Collection中显示一些未定义值的输出
当我尝试打印集合对象时,它会打印收集对象可以';t显示在java、Arraylist和Collection中显示一些未定义值的输出,java,arraylist,collections,Java,Arraylist,Collections,当我尝试打印集合对象时,它会打印Employee@122392Iie92。为什么要打印这个而不是员工名单的详细信息 我的代码: public class Employee { private String name; private String designation; private int employeeId; private int salary; public String getName() { return nam
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);
}
}
公共类员工{
私有字符串名称;
专用字符串指定;
私人国际雇员ID;
私人薪酬;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDesignation(){
返回指定;
}
公共无效设置名称(字符串名称){
这个名称=名称;
}
public int getEmployeeId(){
返回员工ID;
}
public void setEmployeeId(int employeeId){
this.employeeId=employeeId;
}
public int getSalary(){
返回工资;
}
公共无效设置薪资(内部薪资){
这个。薪水=薪水;
}
}
导入java.util.ArrayList;
导入java.util.Scanner;
公共类员工管理{
静态扫描仪sc=新扫描仪(System.in);
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
ArrayList lst=新的ArrayList();
System.out.println(“输入员工人数:”);
int num=sc.nextInt();
EmployeeManagement emp=新员工管理();
emp.addEmployeeName(num,lst);
}
public void addEmployeeName(int num,ArrayList lst){
Employee em=新员工();
对于(int i=0;i它正在使用对象
类的默认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);
}
}
public void addEmployeeName(int num,ArrayList lst){
对于(int i=0;i@KINGBHAI如果这对您有效,请单击anwser旁边的箭头接受答案。其他人将通过查看该答案获得帮助。欢迎来到Stackoverflowhey,但它为不同的员工显示相同的输出。输出:-输入员工人数:2输入员工id:1输入员工姓名:2输入员工名称:3输入员工工资:4输入员工id:5输入员工姓名:6输入员工名称:7输入员工工资:8[6587,6587]有人能告诉我为什么对这个答案投反对票吗?这样我以后就可以避免同样的错误