java - Map.clear() then add map to List -


i try traverse excel2007 data using poi.jar based on jdk1.6.but when seem find strange phenomenon when traverse row(stored hashmap()) , add row data java.util.arraylist .

and starting next iterator,i first clear row data invoking map.clear(), when again invoking arraylist.add() method,this row data overridden older data.

   map<string, string> cellforrow = = new hashmap<string, string>();    list<map<string, string>> rowforsheet = new arraylist<map<string, string>>();    (int j = 0; j < sheet.getphysicalnumberofrows(); j++) {                      row = sheet.getrow(j);                     if (j == 0) {// first row title                         titlerow = row;                         continue;                     }                     if (row != null && titlerow.getphysicalnumberofcells() > 0) {                         // cellforrow = new hashmap<string, string>();                         cellforrow.clear();                         (int k = 0; k < titlerow.getphysicalnumberofcells(); k++) {// cell                             cellforrow.put(getcellvalue(titlerow.getcell(k)), getcellvalue(row.getcell(k)));                         }                     }                     rowforsheet.add(cellforrow);                 } 

next snippets show debug log rowforsheet(list)

[{ =stream, email=xxx,down =stream}, { =stream, email=xxx,down =stream}, { =stream, email=xxx,down =stream}, { =stream, email=xxx,down =stream}, { =stream, email=xxx,down =stream}] 

the later data override older data

did you?

first off, put creation of map inside of loops add new map each time.

list<map<string, string>> rowforsheet = new arraylist<map<string, string>>(); (int j = 0; j < sheet.getphysicalnumberofrows(); j++) {     row = sheet.getrow(j);    if (j == 0) {// first row title       titlerow = row;       continue;    }    map<string, string> cellforrow = = new hashmap<string, string>();    if (row != null && titlerow.getphysicalnumberofcells() > 0) {        (int k = 0; k < titlerow.getphysicalnumberofcells(); k++) {// cell            cellforrow.put(                getcellvalue(titlerow.getcell(k)),                 getcellvalue(row.getcell(k))            );        }    }    rowforsheet.add(cellforrow); } 

now each item in list different map, new data you've added.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -