2 Replies Latest reply: Sep 18, 2013 9:30 AM by geracmos RSS

    Java.util.Vector has wrong data after a loop exit.

    geracmos

      In my code I user java.util.Vector to store tha data that I read from a CSV file.
      The data is read using a BufferedReader in a while loop, and then stored in a two dimensional Vector.
      If I print the vector contents from inside the loop everything is ok, but when the loop exits, then I get
      a vector with the appropriate size (here 7549,8) but containing only the last iteration's data applied to all the elements.

       

      For example I should get EXAMPLE_A but I get EXAMPLE_B.
      How do you explain it?

       

      EXAMPLE_A:

      "eduroam",     00:1C:F9:2A:A5:01     ,21     ,21     ,-87,     -87,     2013-07-15,     10:39:35.203,

      "ENG-WIRELESS",     00:1C:F9:2A:A5:00     ,20     ,20     ,-88     ,-88     ,2013-07-15     ,10:39:34.189,

      "PJ-WIRELESS5"     ,FE:40:D1:9D:5D:E0     ,21     ,28     ,-87     ,-83     ,2013-07-15     ,10:39:36.217,

      "ENG-WIRELESS"     ,00:1C:F9:2A:A5:00     ,20     ,20     ,-88     ,-88     ,2013-07-15     ,10:39:35.203,          <- last line

       

      EXAMPLE_B:

      "ENG-WIRELESS"     ,00:1C:F9:2A:A5:00     ,20     ,20     ,-88     ,-88     ,2013-07-15     ,10:39:35.203,

      "ENG-WIRELESS"     ,00:1C:F9:2A:A5:00     ,20     ,20     ,-88     ,-88     ,2013-07-15     ,10:39:35.203,

      "ENG-WIRELESS"     ,00:1C:F9:2A:A5:00     ,20     ,20     ,-88     ,-88     ,2013-07-15     ,10:39:35.203,

      "ENG-WIRELESS"     ,00:1C:F9:2A:A5:00     ,20     ,20     ,-88     ,-88     ,2013-07-15     ,10:39:35.203,          <- last line

       

      And Here is the Code:

       

      Lines 39 and 44 should print the same thing..

       

       

      import java.io.*;
      
      public class importCSV {
      
          public static void main(String[] args) throws IOException{  
              java.util.Vector <java.util.Vector <String>> data = readCSV();
          }
        
          public static java.util.Vector <java.util.Vector <String>> readCSV() throws IOException {
              java.util.Vector <java.util.Vector<String>> rows = new java.util.Vector <java.util.Vector<String>> ();
              java.util.Vector <String> row1 = new java.util.Vector <String> ();      
              BufferedReader reader = new BufferedReader(new FileReader("src/data.csv"));
              String temp = "";
              String row[];
      
              int quotCounter;
              boolean quotsOpen=false;
              int cnt=0;
            
              while(reader.ready()){
                  quotCounter=0;          
                  row1.clear();
                  int counter=0;
                
                  temp=reader.readLine();
                  row=temp.split(",");
      
                  for(int i=0; i<row.length ;i++){
                      if(quotsOpen)
                          row1.set(counter-1,row1.lastElement().concat(",").concat(row[i]));
                      else{
                          row1.add(row[i]);              
                          counter++;  
                      }
                      for(int j=0;j<row[i].length();j++){
                          if(row[i].charAt(j)== '\"')
                              quotCounter++;
                      }
                      if(quotCounter % 2 == 1) quotsOpen=true;    //even number of <"> characters
                      else quotsOpen=false;
                  }
                  rows.add(row1);
                  System.out.println(rows.get(0).get(0));
                  cnt++;
              }
              reader.close();
              for(int i=0;i<rows.size();i++)
                  System.out.println(rows.get(i).get(0));  
              return rows;
          }
      }
      
      
        • 1. Re: Java.util.Vector has wrong data after a loop exit.
          rp0428
          In my code I user java.util.Vector to store tha data that I read from a CSV file.
          The data is read using a BufferedReader in a while loop, and then stored in a two dimensional Vector.
          If I print the vector contents from inside the loop everything is ok, but when the loop exits, then I get
          a vector with the appropriate size (here 7549,8) but containing only the last iteration's data applied to all the elements.

          EXACTLY! Then Java is working like it is designed to do.

           

          You only allocate ONE row instance:

          java.util.Vector <String> row1 = new java.util.Vector <String> ();       

          Then every time through the loop you put the new contents into it and wipe out the old contents. So naturally when you exit the loop it will only have the contents of the last iteration.

          rows.add(row1);  

          That adds a reference to the SAME 'row1' instance every time through the loop.

           

          You need to allocate a NEW vector (using the 'new' operator) for each row. In other words put the following line of code INSIDE the loop

          java.util.Vector <String> row1 = new java.util.Vector <String> ();       
          • 2. Re: Java.util.Vector has wrong data after a loop exit.
            geracmos

            Thanks a lot rp0428!

             

            I am mostly used to C++ where there is no need to make a new instance in every loop. Thumbs up from me!