我正在尝试处理具有多个附件的彩信。对于我正在创建一个HashMap如下(这不是完全实现,但只有相关部分):HashMap - 正确的用法?
HashMap<String, Integer> hashAttachments = new HashMap<String, Integer>();
int c = 0;
if(atts != null) {
for(Attachment a : atts){
if(a.mimeType.startsWith("image/")){
<some code here>
hashAttachments.put(a.fileName, indx);
}else if(a.mimeType.startsWith("text/")){
<some code here>
hashAttachments.put("text_"+String.valueOf(c++)+".txt",indx);
}
<some more mime types>
} /* for */
我试图处理邮件有附件4 - 两个图像和两个文本,所以我当for循环结束时,希望哈希映射包含4个条目。
我实际上看到的是,在某些时候地图上的一个条目被覆盖,我最终得到3个条目而不是4.可能是什么原因? (钥匙都是独一无二的,不为空,而不是空在所有情况下)编辑
在此先感谢
:每次迭代按键后(看起来很完美,不是我所看到的检查调试器键):
10-16 21:50:01.207: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.207: INFO/System.out(27593): abc.jpg
10-16 21:50:01.207: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.217: INFO/System.out(27593): abc.jpg
10-16 21:50:01.217: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.227: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.227: INFO/System.out(27593): abc.jpg
10-16 21:50:01.227: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.227: INFO/System.out(27593): text_0.txt
10-16 21:50:01.237: INFO/System.out(27593): ~~~~~~~
10-16 21:50:01.237: INFO/System.out(27593): abc.jpg
10-16 21:50:01.237: INFO/System.out(27593): text_1.txt
10-16 21:50:01.237: INFO/System.out(27593): 2010-06-18_12.47.50.jpg
10-16 21:50:01.237: INFO/System.out(27593): text_0.txt
你显示的内容,'块中有什么不对? –
2010-10-16 19:17:06
哪个附件条目在HashMap中被覆盖?只是为了缩小它。 – 2010-10-16 19:19:58
@Mark:有些代码是一些与HashMap问题无关的代码 - 我可以将它注释掉,问题仍然存在 – Asahi 2010-10-16 19:23:09