Greener
Greener

Reputation: 477

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,

Upvotes: 3

Views: 2055

Answers (4)

Achilles
Achilles

Reputation: 11299

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);
}

Upvotes: 11

JasCav
JasCav

Reputation: 34632

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);
       }

Upvotes: 2

Nathan Wheeler
Nathan Wheeler

Reputation: 5932

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);
   }

Upvotes: 2

Bombe
Bombe

Reputation: 83846

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);
}

Upvotes: 2

Related Questions