使用类作为哈希图中的数据类型
我试图在 HashMap
import java.util.HashMap;
public class BidsManager {
// Interger key and bid value in the hash map as an object for bid maneger class
HashMap<Integer, Bid > bidHashMap = new HashMap<Integer,Bid>();
Bid bid;
// a contsuctor that takes zero parameter and initilizes the bit hash map object
public BidsManager() {
this.bidHashMap = bidHashMap;
}
//add bid method that acceptes buyer ID, bid price and bid date
public void addBid(int buyerID, double bidPrice, int bidDate){
// here is where i am not sure with how to put the Bid into the HashMap
bidHashMap.put(buyerID, );
}
}
I am trying to use a class instead of String in the HashMap<Integer, String>,
lets say the class name that i will be using is Bids.java.
and a method called 'addBid' that accepts three parameters: buyer ID, bid price, and bid date. This method must create and a new instance of the Bid class and put in the HashMap attribute the <buyerId, bid> pair.
import java.util.HashMap;
public class BidsManager {
// Interger key and bid value in the hash map as an object for bid maneger class
HashMap<Integer, Bid > bidHashMap = new HashMap<Integer,Bid>();
Bid bid;
// a contsuctor that takes zero parameter and initilizes the bit hash map object
public BidsManager() {
this.bidHashMap = bidHashMap;
}
//add bid method that acceptes buyer ID, bid price and bid date
public void addBid(int buyerID, double bidPrice, int bidDate){
// here is where i am not sure with how to put the Bid into the HashMap
bidHashMap.put(buyerID, );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的最后一句话说明了解决方案:
因此,您需要执行两个步骤:
Bid
对象。Map#put
方法。代码:
Your last sentence states the solution:
So you need to perform two steps:
Bid
object.Map#put
method.Code: