我正在从 excel 电子表格中获取数据,并尝试将每一行添加到地图中,然后将该地图放入数组列表中,但它只是多次放入最后一行这是我拥有的代码:
Iterator<Row> rowIterator = sheet.iterator();
Map<String,Object> cells = new HashMap<String,Object>();
ArrayList list = new ArrayList();
table(cells,rowIterator,list);
public ArrayList table(Map<String,Object> cells, Iterator<Row> rowIterator,ArrayList list) throws Exception
{
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getRowIndex()>=1){
//cell = cellIterator.next();
Object obj = null;
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
obj = cell.getBooleanCellValue();
System.out.print((boolean)obj + "\t\t\t");
cells.put(columnName(cell.getColumnIndex()),obj);
list.add(cells);
break;
case Cell.CELL_TYPE_BLANK:
obj = cell.getStringCellValue();
System.out.print((String)obj + "\t\t\t");
cells.put(columnName(cell.getColumnIndex()),obj);
list.add(cells);
break;
case Cell.CELL_TYPE_NUMERIC:
obj = cell.getNumericCellValue();
System.out.print((double)obj+ "\t\t\t");
cells.put(columnName(cell.getColumnIndex()),obj);
list.add(cells);
break;
case Cell.CELL_TYPE_STRING:
obj = cell.getStringCellValue();
System.out.print((String)obj + "\t\t\t");
cells.put(columnName(cell.getColumnIndex()),obj);
list.add(cells);
break;
}
}
}
System.out.println("");
}
return list;
}
数组列表应该有
[{userid=1, name=dave},{userid=2, name=terry}]
在它里面实际上是什么
[{userid=2, name=terry},{userid=2, name=terry},{userid=2, name=terry},{userid=2, name=terry},{userid=2, name=terry} ,{userid=2, name=terry},{userid=2, name=terry}]
我怎样才能阻止这种情况发生?