【发布时间】:2015-01-30 04:23:04
【问题描述】:
我想遍历 hashmap 的每个键并将其与它下面的每个键进行比较(所以我不会两次比较键)。 我想看看哪些斜坡是相互垂直的。如果斜率的乘积是 -1,它们就是,我将使用这些键对数组列表做一些事情。
到目前为止,我已经使用以下方法构建了地图:
Map<Double, List<Double>> map = new HashMap<Double, List<Double>>();
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(line[4][i] == line[4][j]) {
double distance = 7.0; //distance();
List<Double> array = (map.containsKey(line[4][i])) ? map.get(line[4][i]):new ArrayList<Double>();
array.add(distance);
map.put(line[4][i], array);
// System.out.println(Arrays.asList(array));
// System.out.println(distance);
}
}
}
并尝试通过以下方式遍历地图:
Iterator<Entry<Double, List<Double>>> i = map.entrySet().iterator();
while(i.hasNext()) {
Entry next = i.next();
i.remove();
for(Entry e : map.entrySet()) {
System.out.println(next + " " + e);
//ERROR: The method parseDouble(String) in the type Double is not applicable for the arguments (Object)
if((Double.parseDouble(next.getKey()) * (Double.parseDouble(e.getKey()))) == -1)
System.out.println("pair"); //do something
}
}
但我无法将对象解析为双精度对象,而且我不知道如何继续。任何帮助表示赞赏
【问题讨论】:
-
next.getKey()是双精度数,不用解析,直接用。没有像在HashMap中排序这样的东西,如果你检查键是否相等,你应该使用不同于地图的东西。 -
排序并不重要 afaik。一旦我不再回顾我已经经历过的钥匙,我遍历它的顺序并不重要
-
当我尝试将它用作双精度时,我得到错误的操作数类型“*”
标签: java arraylist iterator hashmap