gpt4 book ai didi

java - HashMap 存储。打印方法未显示正确的输出

转载 作者:行者123 更新时间:2023-11-29 08:06:04 25 4
gpt4 key购买 nike

我在网上查看过,但无法解决我遇到的问题。我正在使用 hashMap 创建一个员工商店,但我不确定我是否应该像我当前版本中那样打印 toString() 或 theEntry.getKey()。使用 getKey() 方法时,我得到错误的输出:

*公司员工。*** 员工姓名:James O' Carroll员工编号:James O' Carroll电子邮箱:James O' Carroll

正如您在 MainApp 中看到的,我想要 Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));成为输出。

我将粘贴我的代码:

//MainApp.
public class MainApp
{

public static void main(String[] args)
{
new MainApp().start();

}
public void start()
{
EmployeeStore Store = new EmployeeStore();
Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));
Store.print();

}

}

//Employee
//Imports:

//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
private String employeeName;
private int employeeId;
private String employeeEmail;
//********************************************************************
//Constructor.
public Employee(String employeeName, int employeeId, String employeeEmail)
{
this.employeeName = employeeName;
this.employeeId = employeeId;
this.employeeEmail = employeeEmail;
}
//********************************************************************
//Getters.
public String getEmployeeEmail() {
return employeeEmail;
}
public void setEmployeeEmail(String employeeEmail) {
this.employeeEmail = employeeEmail;
}
public String getEmployeeName() {
return employeeName;
}
public int getEmployeeId() {
return employeeId;
}
//********************************************************************
//toString method.
public String toString() {
return "Employee [employeeName=" + employeeName + ", employeeId="
+ employeeId + ", employeeEmail=" + employeeEmail + "]";
}
//********************************************************************





}

//EmployeeStore.
//Imports.
import java.util.HashMap;
//********************************************************************
import java.util.Map;

public class EmployeeStore
{
HashMap<String, Employee> map;

//Constructor.
public EmployeeStore()
{
map = new HashMap<String,Employee>();
}
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
public void add(Employee obj)
{

map.put(obj.getEmployeeName(), obj);
}
//********************************************************************
//Remove from the Hashmap : Employee.
public void remove(String key)
{
//Remove the Employee by name.
map.remove(key);
}
//********************************************************************
//Print the Hashmap : Employee.
public void print()
{
System.out.println("\n********Employee's in the Company.********");
for(Map.Entry<String, Employee> theEntry : map.entrySet())
{
System.out.println("Employee Name:\t" + theEntry.getKey());
System.out.println("Employee Id:\t" + theEntry.getKey());
System.out.println("E-mail:\t "+ theEntry.getKey());

}
}


//********************************************************************
//********************************************************************


}

最佳答案

这显然是错误的:

for(Map.Entry<String, Employee> theEntry : map.entrySet())
{
System.out.println("Employee Name:\t" + theEntry.getKey());
System.out.println("Employee Id:\t" + theEntry.getKey());
System.out.println("E-mail:\t "+ theEntry.getKey());
}

您要打印相同的值 3 次 - 怎么可能是姓名、ID 电子邮件地址?您正在打印条目中的,它始终是名称。您需要条目的

你想要这个:

// This assumes you've renamed all the properties in Employee to be more
// sensible - there's no point in including the "employee" part - we already
// know we're calling a method on Employee
for (Employee employee : map.values())
{
System.out.println("Employee Name:\t" + employee.getName());
System.out.println("Employee Id:\t" + employee.getId());
System.out.println("E-mail:\t "+ employee.getEmail());
}

(或者当然只是打印employee。这取决于你想要的输出是什么。)

关于java - HashMap 存储。打印方法未显示正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11172271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com