我有这个简单的代码
public static HashMap<String, String> FixDataBeforeSenddd(final ImageManager image1,
final ImageManager image2,
final ImageManager image3,
final ImageManager video)
{
int index = 1;
boolean hasChanged = false;
HashMap<String, String> hash = new HashMap<String, String>();
ArrayList<ImageManager> list = new ArrayList<ImageManager>();
list.add(image1);
list.add(image2);
list.add(image3);
for (int i = 0; i < list.size(); i++)
if (list.get(i).getHasChanged() == true)
hasChanged = true;
if (hasChanged == true)
for (int i = 0; i < list.size(); i++)
if (list.get(i).getHasImage())
{
hash.put(index + "", list.get(i).getFilePath());
index++;
}
if (video.getHasChanged() == true)
if (video.getHasImage() == true)
{
hash.put("videoImage", video.getVideoThumlPath());
hash.put("video", video.getVideoFilePath());
}
return hash;// mh;
}
现在的问题是每个hash.put都会覆盖现有的Key,让我们假设我在每个imageManager对象中都有图像所以第一个for循环结果应该是这样的:
{"1"="SomePath...","2"="SomePath...","3"="SomePath...",//end for loop,"video"="SomePath","videoImage"="SomePath"}
但实际结果如下:
"video"="SomePath","videoImage"="SomePath"
和id视频ImageManager是空的但不是其他结果是: &#34; 3&#34; =&#34; SomePath ...&#34;
为什么会覆盖不同的密钥?
修改
当我调试时,我注意到被覆盖的密钥进入&#34; next&#34;使用Watcher的单元格
答案 0 :(得分:0)
hasChanged
将始终设置为列表中最后一项的值。如果要在列表项的任何更改后将hasChanged
设置为true,请将for循环更改为:
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getHasChanged()) {
hasChanged |= true;
}
}
还添加了大括号以提高可读性。