2015-04-15 131 views
0

我需要连接BasicDBList中的所有BasicDBObject。每次循环运行时,我的BasicDBObject只包含ONE json元素,并退出我的BasicDBList不包含任何内容。
为什么会发生这种情况?将dbLinha.clear()放在避免重复但每次循环运行时评估代码BasicDBList包含重复!为什么我不能连接JSON?

public BasicDBList readMetadados(Planilha planilha) { 
     List<String> cabecalho = new ArrayList<>(); 
     int linhaReferencia = 0; 
     BasicDBObject dbLinha = new BasicDBObject(); 
     BasicDBList listLinha = new BasicDBList(); 

     try { 
      InputStream planilhaFile = new FileInputStream(FileUtils.getFile(UPLOAD_PATH, planilha.getPath())); 
      Sheet linhaInicial = new XSSFWorkbook(planilhaFile).getSheetAt(0); 
      Iterator<Row> rowIterator = linhaInicial.iterator(); 

      while (rowIterator.hasNext()) { 
       Row row = rowIterator.next(); 
       Iterator<Cell> cellIterator = row.cellIterator(); 
       while (cellIterator.hasNext()) { 
        Cell cell = cellIterator.next(); 
        try { 
         if (cell.getCellType() != 3) { 

          if (cell.getCellType() == 1) { 
           if ("Veículo".equals(cell.getStringCellValue())) { 
            linhaReferencia = cell.getRow().getRowNum(); 
            cabecalho.add(cell.getStringCellValue()); 
            while (cellIterator.hasNext()) { 
             cabecalho.add(cellIterator.next().getStringCellValue()); 
            } 
            break; 
           } 
          } 

          if (linhaReferencia != 0) { 
           switch (cell.getCellType()) { 
            case Cell.CELL_TYPE_FORMULA: 
             dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getCellFormula()); 
             break; 
            case Cell.CELL_TYPE_BOOLEAN: 
             dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getBooleanCellValue()); 
             break; 
            case Cell.CELL_TYPE_NUMERIC: 
             dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getNumericCellValue()); 
             break; 
            default: 
             dbLinha.append(cabecalho.get(cell.getColumnIndex()), cell.getStringCellValue()); 
           } 
          } 

         } 
        } catch (IllegalStateException e) { 
         Log.info(this, "Erro ao obter valor da linha [{}] e coluna [{}]", cell.getRow().getRowNum(), cell.getColumnIndex()); 
        } 
       } 
       if (!dbLinha.isEmpty()) { 
        for(int i = 0; i < cabecalho.size(); i++){ 
         if(!dbLinha.containsKey(cabecalho.get(i))){ 
          dbLinha.append(cabecalho.get(i), " "); 
         } 
        } 
        listLinha.add(dbLinha); 
        dbLinha.clear(); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      Log.error(this, "Erro ao processar planilha: Planilha não encontrada.", e); 
     } catch (IOException e) { 
      Log.error(this, "Erro ao processar planilha.", e); 
     } 
     System.out.println(listLinha.toString()); 
     return listLinha; 
    } 

输出

[ { } , { } , { } , { } , { } , { }] 

BasicDBList的内容你第一次运行时是正确的,第二次开始复制和添加的anteriormentes取代。

BasicDBList的第一次运行循环中的值( “如果(!dbLinha.isEmpty())”) enter image description here

第二次 enter image description here

+4

如果你可以将这个提取成一个* short *但是* complete *程序来证明这个问题,那真的很有帮助... –

+0

为了清晰起见编辑了这个问题。主要问题是连接“listLinha”中的所有JSON –

+0

您仍然没有提供一个简短但完整的程序来演示问题,尽管... –

回答

2

您正在尝试使用保存对象clear并且一遍又一遍地使用相同的对象(dbLinha)。这是行不通的。

当您添加一个对象列表,它增加了参考该对象,而不是一个该对象的复制到列表中。因此,基本上,第一次添加的内容是对dbLinha对象的引用,现在您已将列表中的第一项指向与dbLinha设置为相同的对象。您可以拨打dbLinha.clear()

这意味着存储在您的列表中的引用是相同的,现在将显示一个空对象。然后,您将另一行读入同一对象,将其他引用添加到列表中,然后再清除它。

您的列表中充满了对您正在重新使用的单个对象的引用。这里是正在发生的演示:

Demonstration of the process

如果你想留住你的对象,你必须使用new,不clear。您必须创建一个新对象来存储下一个数据位,因为添加到列表中不会创建副本,只是引用。所以你基本上必须让你添加的引用指向旧对象,并从一个新对象开始。

+0

令人惊叹的响应。我真的认为列表中有一个副本。 –

相关问题