在EmployeeSearch的主要方法中,创建一个包含多个Employee对象的数组,其中包括一个与您的第一个&姓氏,并使用您的电子邮件ID作为ID。使用addEmployees方法将每个Employee放入Map和Set中。使用增强的for循环。 (您可以使用getID方法检索员工的ID以用作Map的关键字。)
搜索地图。提示用户输入搜索地图所需的任何信息。使用get方法搜索。
我尝试了几种方法,不确定哪种方式是正确的;我评论了我认为可能的解决方案,但我不确定还能做什么。
我是否正确创建了对象数组?像这样 - ArrayList employees = new ArrayList();或员工员工=新员工(" John"," Bowman"," 970");然后我需要搜索,这是addEmployees方法应该做的,但我一直在收到错误。
public class EmployeeSearch {
public static void main(String[] args) {
public Set<Employee> empSet = new HashSet<Employee>();
public Map<String, Employee> empMap = new HashMap<String, Employee>();
// empMap.put("John", "970");
ArrayList<String> employees = new ArrayList<String>();
//empMap.put("John", employees);
employees.add("John");
employees.add("Bowman");
employees.add("970");
System.out.println("Employee:");
for (String employeeName : employees){
System.out.println(employeeName);
}
//Employee employees = new Employee("John", "Bowman", "970");
//empSet.add("John", "Bowman", "970");
}
public void addEmployees(Employee[] employees) {
// empSet.add(employees[0]);
// empMap.put(employees[0].getID(),employees[0]);
// for(empSet.add(employees[e])):empMap.put(employees[e]){
}
}
---------------------- UPDATE ------------------------ --------------------
public class EmployeeSearch {
public static void main(String[] args) {
Set<Employee> empSet = new HashSet<Employee>();
public Map<String, Employee> empMap = new HashMap<String, Employee>();
public Employee emp1 = new Employee("John", "Bowman", "970");
ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
empMap.put("970", emp1);
for (Employee employeeName : employees) {
System.out.println(employeeName);
}
}
public void addEmployees(Employee[] employees){
empSet.add(addEmployees(employees));
empMap.put(employees.getID(), addEmployees(employees));
Employee searchedFor = empMap.get(id);
if (searchedFor == null) {
System.out.println();
}else{
System.out.println();
}
}
}
我一直在&#34;无法解决符号&#34;错误和它说&#34;使用addEmployees方法将每个Employee放入Map和Set。&#34;我很确定我在main中做了这个,并且无法弄清楚如何将它放在addEmployees方法中。
答案 0 :(得分:1)
如果您尝试将员工添加到阵列列表,您只需要:
//Create a single employee
Employee employee = new Employee("John", "Doe", "Address");
//Create array list
ArrayList<Employee> employees = new ArrayList<Employee>();
//Add employee to list
employees.add(employee);
//repeat as needed for each employee
如果你想要一个哈希映射:
//Create a single employee
Employee employee = new Employee("John", "Doe", "Address");
//Create array list
HashMap<String, Employee> employees = new ArrayList<String, Employee>();
//Add employee to list
employees.put("email@example.com", employee);
//repeat as needed for each employee
如果是我,我会使用Hashmap,因为在这种情况下,您可以通过以下方式轻松搜索它:
//search for employee
if( employees.containsKey("email@example.com")){
//retrieve employee object
Employee myGuy = employees.get("email@example.com");
}
答案 1 :(得分:1)
基本诀窍是 - 你猜错了:
Employee oneEmployee = new Employee("John", "Bowman", "970");
empSet.add(oneEmployee);
empMap.put(oneEmployee.getID(), oneEmployee);
这要求:你的Employee
类应该有适当的3参数构造函数(可能已经有,否则写一个)。除了empSet
之外,您还需要声明empMap
。
编辑:
我从您的评论中了解到,您需要将一组员工传递给addEmployees
方法。有很多方法可以创建和填充这样的数组。最简单的方法是使用数组初始值设定项:
Employee[] allEmployees = { new Employee("John", "Bowman", "970"),
new Employee("John", "Rawsick", "6"),
new Employee("BlackHat", "Samurai", "95")
};
根据需要制作阵列。
在addEmployees
方法中,您可以使用所谓的增强型for循环(分配中的另一个要求)来遍历员工,并使用基本技巧将每个人员添加到集合和地图中我在顶部展示了。
按ID搜索empoyee是您地图中的查找。假设您有String id
包含用户输入 - 您已被告知使用get
方法:
Employee employeeSearchedFor = empMap.get(id);
if (employeeSearchedFor == null) {
System.out.println("No employee with ID " + id + " found";
} else {
// print the employee
}
免责声明:我没有编译和运行代码,某处可能存在拼写错误。