我想遍历地图中的几个条目...Java的地图迭代
在wizard()
,我把4名映射在map
,然后发送地图有两个输入cancer
和test
一起被calcuated ...
public int wizard() {
Map<String, String> map = new HashMap<String, String>();
//historical data of having cancer given test result...
map.put("+cancer", "+test");
map.put("-cancer", "+test");
map.put("-cancer", "-test");
map.put("+cancer", "+test");
String cancer = "+cancer";
String test = "+test";
//send historical data to be calculated...
return calculate(cancer, test, map);
}
这里,calcuate()
遍历地图索引寻找到两个输入cancer
和test
比赛,然后返回的条件概率:
public int calculate(String cancer, String test, Map<String, String> map) {
int tests = 0;
int both = 0;
System.out.println("Cancer: " + cancer + "; Test: " + test);
for (int i = 0; i <= map.size(); i++) {
if (map.containsValue(test)) {
tests++;
if (map.containsValue(cancer)) {
both++;
}
}
}
System.out.println("{Cancer & Tests}: " + both + "; Tests: " + tests);
return both/tests;
}
输出:
Cancer: +cancer; Test: +test
{Cancer & Tests}: 0; {Tests}: 3
P(Cancer|Test): 0
你可以看到,both++
没有增加(又名:{Cancer & Tests}
:不应该是0
),因此P(Cancer|Test)
没有给予正确的答案。
这是为什么?我是否在地图上错误地迭代?
好,嗯,是有一些其他的方式做一个关联数组,如:'$ DATA [0] =阵列( “+癌症”,“+测试“);','$ Data [1] = array(” - cancer“,”-test“);'?我只想遍历关联数组的每个部分,并查看这些值是否包含在特定的数组索引处。 – Growler
我已经用可能的解决方案修改了我的答案。 – rgettman