从 HashMap 插入和搜索数据
我的解决方案有问题,我不太明白。如果有人能够引导我朝正确的方向前进并使其发挥作用,我将非常感激。当我调用我的方法时似乎什么也没有发生。
以下是工作参数的 setter 和 getter:
public class Worker {
private String id;
private String name;
private String location;
public Flight(String id, String name, String location) {
this.id = id;
this.name = name;
this.location = location;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
我想要 WorkerDB 类上的工作数据以及处理搜索功能并将新工作插入到 HashMap 的方法。
public class WorkerDB {
HashMap<Integer, Worker> workers = new HashMap<Integer, Worker>();
public WorkerDB() {
workers.put(1, new Worker("W01", "John", "Workshop"));
workers.put(2, new Worker("W02", "Steve", "Office"));
workers.put(3, new Worker("W03", "Bob", "Gate"));
}
public String InsertWorkerData(String [] workerDataArray) {
int sizeOfHashMap = Integer.parseInt(workerDataArray[0]);
workers.put(sizeOfHashMap, new Worker(workerDataArray[1], workerDataArray[2], workerDataArray[3]);
return "Worker data inserted successfully";
}
public String searchWorker(String workerID) {
String search = "Worker with ID: " + workerID + ", not found";
for (int i = 1; i < workers.size(); i++) {
if(workers.get(i).getID().equals(workerID)) {
search = workers.get(i).getID() + " " + workers.get(i).getName() + " " + workers.get(i).getLocation();
}
}
return search;
}
这里
我只是调用 WorkerDB 类中的方法。
public class Interface {
public static void main(String[] args) {
WorkerDB workers = new WorkerDB();
String[] testArray = {"4", "W04", "George", "Garage"};
workers.searchWorker("W01");
}
}
There's something wrong with my solution and I can't quite figure it out. I would very much appreciate it, if someone could steer me in the right direction and get this working. When I'm calling my methods nothing seems to happen.
Here's the setters and getters for the worker parameters:
public class Worker {
private String id;
private String name;
private String location;
public Flight(String id, String name, String location) {
this.id = id;
this.name = name;
this.location = location;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
I want to have the worker data on the WorkerDB class with the methods to handle the search function and inserting new workers to the HashMap.
public class WorkerDB {
HashMap<Integer, Worker> workers = new HashMap<Integer, Worker>();
public WorkerDB() {
workers.put(1, new Worker("W01", "John", "Workshop"));
workers.put(2, new Worker("W02", "Steve", "Office"));
workers.put(3, new Worker("W03", "Bob", "Gate"));
}
public String InsertWorkerData(String [] workerDataArray) {
int sizeOfHashMap = Integer.parseInt(workerDataArray[0]);
workers.put(sizeOfHashMap, new Worker(workerDataArray[1], workerDataArray[2], workerDataArray[3]);
return "Worker data inserted successfully";
}
public String searchWorker(String workerID) {
String search = "Worker with ID: " + workerID + ", not found";
for (int i = 1; i < workers.size(); i++) {
if(workers.get(i).getID().equals(workerID)) {
search = workers.get(i).getID() + " " + workers.get(i).getName() + " " + workers.get(i).getLocation();
}
}
return search;
}
}
Here I'm just calling the methods from the WorkerDB class.
public class Interface {
public static void main(String[] args) {
WorkerDB workers = new WorkerDB();
String[] testArray = {"4", "W04", "George", "Garage"};
workers.searchWorker("W01");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的代码中有一些拼写错误:
首先,在您的
Worker
类中,您需要定义构造函数,使其为public Worker()
并且不是 public Flight() :此外,您的 WorkerDB 类中的括号放错了位置:
此外,由于您使用 return 语句返回字符串,因此我们需要使用在我们的 main 方法中输出它的 print 语句:
结果:
W01 John Workshop
此外,您应该从 0 开始直到(并包括)HashMap 的长度,因此使用
<=
:我希望这有助于回答您的问题!如果您需要任何进一步的详细信息或说明,请告诉我:)
I think you just have a few typos in your code:
First of all, in your
Worker
class, you need to define the constructor so that it ispublic Worker()
and notpublic Flight()
:Additionally, you had a misplaced parenthesis in your
WorkerDB
class:Also, since you are using a return statement to return the String, we need to use a print statement to output it in our main method:
Result:
W01 John Workshop
Additionally, you should be going up until (and including) the length of the HashMap since you start at 0, so use
<=
:I hope this helped answer your question! Please let me know if you need any further details or clarification :)