开发者

arraylist with objects does not hold the values

I have a class called Technician

   public class Technician {
     private String empLName;
     private String empFName;
     private int empId;
   //I skipped all setters and getters      
  }

In other class I retrieve all technicians names and load them into the array list.

   Technician empl = new Technician();
   ArrayList <Technician> employees = new ArrayList<Technician>();
   //...skip code related to database
   // rs is ResultSet

      while (rs.next()){

          empl.setEmpFName(rs.getString("EMP_LNAME"));
          empl.setEmpLName(rs.getString("EMP_FNAME"));
          empl.setEmpId(rs.getInt("EMP_ID"));
          employees.add(empl开发者_如何学编程);
       }

When I debug I see correct values being retrieved from database. At first iteration of the while loop my empl object gets a value of the first employee in the database and it is being stored in employees ArrayList. At second iteration, the first object in employees ArrayList gets overwritten with the value of the second employee. Thus, I have two employees in my ArrayList with the same lastname , first name. At the third iteration, the same story, two employees in employees ArrayList are overwritten with value of the third employee from the database.

I would appreciate if any suggestions how to fix my code. Thanks,


You need to re-instantiate empl within the while loop.

The problem with your code is that empl is a reference type. It points to a block of memory. When you set the values of empl's properties it is simply overwriting the values stored in that block of memory instead of creating new memory to hold the different values. The ArrayList is simply holding N cells referring to the same block of memory referenced by empl.

Fix:

 while (rs.next()){
   Technician empl = new Technician();
   empl.setEmpFName(rs.getString("EMP_LNAME"));          
   empl.setEmpLName(rs.getString("EMP_FNAME"));          
   empl.setEmpId(rs.getInt("EMP_ID"));          
   employees.add(empl);
}


You keep changing and adding the same instance to the list. You need to create a new instance at every loop.

while (rs.next()) {
    empl = new Technician();
    empl.setEmpFName(rs.getString("EMP_LNAME"));
    empl.setEmpLName(rs.getString("EMP_FNAME"));
    empl.setEmpId(rs.getInt("EMP_ID"));
    employees.add(empl);
}


You're putting the SAME empl into employees each time, and then changing the value of empl for each row. Do this instead:

   ArrayList <Technician> employees = new ArrayList<Technician>();
   //...skip code related to database
   // rs is ResultSet

   while (rs.next()){
       Technician empl = new Technician();

       empl.setEmpFName(rs.getString("EMP_LNAME"));
       empl.setEmpLName(rs.getString("EMP_FNAME"));
       empl.setEmpId(rs.getInt("EMP_ID"));
       employees.add(empl);
   }


The reason this is happening is because the empl is the same reference every time you loop through your array. Instead, you have to initialize a new empl object.

Technician empl = new Technician();
   ArrayList <Technician> employees = new ArrayList<Technician>();
   //...skip code related to database
   // rs is ResultSet

      while (rs.next()){
          empl = new Technician();
          empl.setEmpFName(rs.getString("EMP_LNAME"));
          empl.setEmpLName(rs.getString("EMP_FNAME"));
          empl.setEmpId(rs.getInt("EMP_ID"));
          employees.add(empl);
       }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜