List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> item = new HashMap<String, Object>();
data.clear();
item.clear();
int i = 0;
while (i < 5){
item.put("id", i);
i++;
out.println("id: " + item.get("id"));
out.println("--------------------------");
data.add(item);
}
for(i=0 ; i<5 ; i++){
out.println("print data[" + i + "]" + data.get(i));
}
的那
結果是:的servlet的ArrayList和HashMap
ID:0
------------------------- -
ID:1個
--------------------------
ID:2
--------- -----------------
id:3
--------------------------
id:4
--------------------------
print data [0] {id = 4}
print data [1 ] {ID = 4}
打印數據[2] {ID = 4}
打印數據[3] {ID = 4}
打印數據[4] {ID = 4}
爲什麼只有最後一個元素被存儲?