1
编辑里面的值:解决。我需要定义的顺序切换为整数,动作排序一个TreeMap的java
我已经创建了一个一到一个TreeMap中,其中键是枚举和的值是整数。我想循环从最小值到最大值,并且遇到一些麻烦。
功能当我运行下面的for循环不按升序排列打印值它创建地图
public TreeMap<Action,Integer> mapMoves(int position, ArrayList<Action> directions){
TreeMap<Action,Integer> map = new TreeMap<>();
for(Action a : directions){
switch(a){
case UP:
map.put(a,board.get(position-3));
break;
case DOWN:
map.put(a,board.get(position+3));
break;
case LEFT:
map.put(a,board.get(position-1));
break;
case RIGHT:
map.put(a,board.get(position+1));
break;
}
}
return map;
}
。
TreeMap<Action, Integer> map = current.hashMoves(emptyIndex, possibleMoves);
for (Map.Entry<Action, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
,因为你的关键是 “行动”,而不是数量 – IddoE
如果我可以这样说,我认为你正在尝试使用一个数据结构来实现它没有为之而作。为了给出一个建议,你可以对'directions'数组进行排序,并使TreeMap指向数组中的位置。这样,您就拥有了两全其美的优势:您可以使用TreeMap快速获取使用键搜索的值,并且只需使用排序的数组即可按顺序打印所有值。 – Discoverer98
好的。直觉上似乎倒退到我,因为密钥通常字符串和值是数字 –