今天写代码发现提示
代码如下:
private Q_learningRouter get_next_state(Q_learningRouter destination) {
HashMap<Q_learningRouter,Double> nextHosts = null;
double max1=0;
Q_learningRouter next = null;
for(Connection con : getConnections()) {
DTNHost other = con.getOtherNode(getHost());
Q_learningRouter otherRouter = (Q_learningRouter)other.getRouter();
if(otherRouter.isTransferring()) {
continue;
}else {
nextHosts.put(otherRouter, otherRouter.get_max_q_value_to_destination(destination));
}
}//获得当前连接的router集合
if(!nextHosts.isEmpty()) {
for(Q_learningRouter key : nextHosts.keySet()) {//此时的router是可以直接传输的router,此时遍历选择最大q_value即可
if(key.get_max_q_value_to_destination(destination)>max1) {
max1 = key.get_max_q_value_to_destination(destination);
next = key;
}
}
}
return next;
}//得到下一状态,通过下一节点返回的q值进行选择;通过当前连接的router集合,通过router集合遍历得到最大q值,获取nextState;
上面的nextHosts提示该warning
原因:HashMap<Q_learningRouter,Double> nextHosts = null;
解决办法: HashMap<Q_learningRouter,Double> nextHosts = new HashMap<Q_learningRouter,Double>();
此时的HashMap并没有创建,所以不能成功put,需要new一个对象。