我看了网上,我还没有能够解决我有问题。我正在使用哈希映射制作一个员工商店,但我不知道我是否应该打印toString()或theEntry.getKey(),就像我在当前版本中一样。当使用信息getKey()方法,我得到错误的输出是:HashMapStore。打印方法没有显示正确的输出
*员工在公司。 *** 员工姓名:詹姆斯O“卡罗尔 员工标识:詹姆斯O”卡罗尔 电子邮件:詹姆斯O”卡罗尔
正如你可以在MainApp看到我想Store.add(新员工(“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());
}
}
//********************************************************************
//********************************************************************
}
是的工作,谢谢你。我从一些笔记中得到了这段代码,它通过使用它来工作:System.out.println(“\ n ----- FLIGHTS IN THE AIR ----”); \t \t为(Map.Entry的<字符串,平面> theEntry:map.entrySet()) \t \t { \t \t \t的System.out.println( “飞行时间:\ t” 的+ theEntry.getKey()); System.out.println(“Details:\ t”+ theEntry.getValue());}; \t \t} \t}锄头这样做的工作。 – Pendo826
+1。你最后一次得到赞誉的时间是什么时候? :) –
@SoboLAN:哦,这是一个罕见的一天,我没有*任何* downvotes。有时候是应得的,有时候甚至没有解释... –