【问题标题】:arraylist with objects does not hold the values带有对象的数组列表不保存值
【发布时间】:2009-12-03 20:32:08
【问题描述】:

我有一门叫技术员的课

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

在其他类中,我检索所有技术人员的姓名并将它们加载到数组列表中。

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

当我调试时,我看到从数据库中检索到正确的值。 在 while 循环的第一次迭代中,我的 empl 对象的值是 数据库中的第一个员工,它被存储在员工 ArrayList 中。 在第二次迭代中,employees ArrayList 中的第一个对象被第二个employee 的值覆盖。因此,我的 ArrayList 中有两个员工具有相同的 lastname 和 first name。 在第三次迭代中,同样的故事,employees ArrayList 中的两个员工被覆盖为 数据库中第三名员工的值。

如果对如何修复我的代码有任何建议,我将不胜感激。 谢谢,

【问题讨论】:

  • +2,如果可以的话,可以完美地发布 clean 代码!

标签: java


【解决方案1】:

您需要在 while 循环中重新实例化 empl。

您的代码的问题在于 empl 是一个引用类型。它指向一块内存。当您设置 empl 属性的值时,它只是覆盖存储在该内存块中的值,而不是创建新内存来保存不同的值。 ArrayList 只是保存了 N 个单元,这些单元引用了 empl 引用的同一块内存。

修复:

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

【讨论】:

  • 我自己也多次犯过同样的错误。
  • 似乎每个人的答案都一样。
【解决方案2】:

您不断更改并将相同的实例添加到列表中。您需要在每个循环中创建一个新实例。

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

【讨论】:

    【解决方案3】:

    您每次都将相同的 empl 放入员工中,然后更改每行的 empl 值。改为这样做:

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

    【讨论】:

      【解决方案4】:

      发生这种情况的原因是,每次循环遍历数组时,empl 都是同一个引用。相反,您必须初始化一个新的 empl 对象。

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

      【讨论】:

        猜你喜欢
        • 2011-05-22
        • 2020-01-16
        • 2012-04-05
        • 1970-01-01
        • 2019-06-13
        • 1970-01-01
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        相关资源
        最近更新 更多