-1

我有点新,正在做一个项目。我有一个 ArrayList,我在其中调用 .add 来添加元素。我的添加是以某种方式将相同的记录添加到数组中的所有元素。以下是我与该问题相关的代码片段。任何想法为什么我两次获得相同的记录?

ArrayList 和相关声明:

public class dlg_TEntity extends javax.swing.JDialog {

    ArrayList<Struct_TEntity> structRec = new ArrayList<>(); 

    private static class Struct_TEntity {
        String s_id = null;
        String s_desc = null;
        String s_type = null;
        String s_busType = null;
        String s_tin = null;

        private Struct_TEntity() {};
        private Struct_TEntity(String i,String d, String t,String b, String n) {
            set_ID(i);
            set_Desc(d);
            set_Type(t);
            set_BusType(b);
            set_TIN(n);
        };

        private String get_ID() {
            return s_id;
        };   
        private void set_ID(String a) {
            s_id = a;
        };
        private String get_Desc() {
            return s_desc;
        };
        private void set_Desc(String a) {
            s_desc = a;
        };
        private String get_Type() {
            return s_type;
        };
        private void set_Type(String a) {
            s_type = a;
        };
        private String get_BusType() {
            return s_busType;
        };
        private void set_BusType(String a) {
            s_busType = a;
        };
        private String get_TIN() {
            return s_tin;
        };
        private void set_TIN(String a) {
            s_tin = a;
        };

    }

    /*METHOD DECLARATION*/

    private void initTEntityRec() throws Exception {

        Connection cn = null;
        String SQL = null;
        CallableStatement cs = null;
        ResultSet rs = null;
        Struct_TEntity entityRec = new Struct_TEntity();


        *// database call code working fine... relevant snipet code giving problems shown below...*

        rs = cs.executeQuery();   

        structRec.clear();
        while (rs.next() ) {               
          entityRec.set_ID(rs.getString(1) );
          entityRec.set_Desc(rs.getString(2) );
          entityRec.set_Type(rs.getString(3) );
          if (rs.getString(4)==null){ entityRec.set_BusType(""); } else { entityRec.set_BusType(rs.getString(4) ); };
          if (rs.getString(5)==null){ entityRec.set_TIN(""); } else { entityRec.set_TIN(rs.getString(5)); };                                 
          structRec.add(entityRec);


         System.out.println(getCurrDateTimeStamp() + 
             "Populate entityRec with database recordset results  : Record: " + 
                      structRec.lastIndexOf(entityRec) );  
         System.out.println(getCurrDateTimeStamp() + "j index               : " + j );
         System.out.println(getCurrDateTimeStamp() + "entityRec.ID          : " + entityRec.get_ID() );
         System.out.println(getCurrDateTimeStamp() + "entityRec.Desc        : " + entityRec.get_Desc() );
         System.out.println(getCurrDateTimeStamp() + "entityRec.Type        : " + entityRec.get_Type() );
         System.out.println(getCurrDateTimeStamp() + "entityRec.BusType     : " + entityRec.get_BusType() );
         System.out.println(getCurrDateTimeStamp() + "entityRec.TIN         : " + entityRec.get_TIN() );
         System.out.println(getCurrDateTimeStamp() );


    }; // Close while...

    for(j=0; j<structRec.size(); j++){
        System.out.println(getCurrDateTimeStamp() + "structRec index j             :" + j);
        System.out.println(getCurrDateTimeStamp() + "structRec.get(j).get_ID()     :" + structRec.get(j).get_ID() );
        System.out.println(getCurrDateTimeStamp() + "structRec.get(j).get_Desc()   :" + structRec.get(j).get_Desc() );
        System.out.println(getCurrDateTimeStamp() + "structRec.get(j).get_Type()   :" + structRec.get(j).get_Type() );
        System.out.println(getCurrDateTimeStamp() + "structRec.get(j).get_BusType():" + structRec.get(j).get_BusType() );
        System.out.println(getCurrDateTimeStamp() + "structRec.get(j).get_TIN()    :" + structRec.get(j).get_TIN() );        
    };

我的系统输出显示我的 ArrayList structRec 记录与我添加的记录集不匹配。我的 structRec.add(entityRec)' 语句有问题吗?

系统输出

2013-05-16 03:40:53:042 || 使用数据库记录集结果填充 entityRec:记录:0

2013-05-16 03:40:53:042 || entityRec.ID : test1

2013-05-16 03:40:53:042 || entityRec.Desc : test1_desc

2013-05-16 03:40:53:042 || 实体记录类型:P

2013-05-16 03:40:53:042 || entityRec.BusType :

2013-05-16 03:40:53:042 || entityRec.TIN : 123456789

2013-05-16 03:40:53:042 ||

2013-05-16 03:40:53:042 || 使用数据库记录集结果填充 entityRec:记录:1

2013-05-16 03:40:53:042 || entityRec.ID : test2

2013-05-16 03:40:53:042 || entityRec.Desc : test2_desc2

2013-05-16 03:40:53:042 || entityRec.Type : B

2013-05-16 03:40:53:042 || entityRec.BusType : 不以营利为目的

2013-05-16 03:40:53:042 || entityRec.TIN : 987654321

2013-05-16 03:40:53:042 ||

2013-05-16 03:40:53:042 || structRec 索引 j :0

2013-05-16 03:40:53:042 || structRec.get(j).get_ID() :test2

2013-05-16 03:40:53:042 || structRec.get(j).get_Desc() :test2_desc2

2013-05-16 03:40:53:042 || structRec.get(j).get_Type() :B

2013-05-16 03:40:53:042 || structRec.get(j).get_BusType() : 不以盈利为目的

2013-05-16 03:40:53:042 || structRec.get(j).get_TIN() :987654321

2013-05-16 03:40:53:042 || structRec 索引 j :1

2013-05-16 03:40:53:058 || structRec.get(j).get_ID() :test2

2013-05-16 03:40:53:058 || structRec.get(j).get_Desc() :test2_desc2

2013-05-16 03:40:53:058 || structRec.get(j).get_Type() :B

2013-05-16 03:40:53:058 || structRec.get(j).get_BusType() : 不以盈利为目的

2013-05-16 03:40:53:058 || structRec.get(j).get_TIN() :987654321

4

2 回答 2

3

entityRec每次循环时都必须创建一个新实例,否则将修改前一个实例。

尝试 :

while (rs.next() ) {  
  Struct_TEntity entityRec = new Struct_TEntity(); //here create a new entity         
  entityRec.set_ID(rs.getString(1) );
  entityRec.set_Desc(rs.getString(2) );
  entityRec.set_Type(rs.getString(3) );
  if (rs.getString(4)==null){ entityRec.set_BusType(""); } else { entityRec.set_BusType(rs.getString(4) ); };
  if (rs.getString(5)==null){ entityRec.set_TIN(""); } else { entityRec.set_TIN(rs.getString(5)); };                                 
  structRec.add(entityRec);


 System.out.println(getCurrDateTimeStamp() + 
 "Populate entityRec with database recordset results  : Record: " + 
                      structRec.lastIndexOf(entityRec) );  
 System.out.println(getCurrDateTimeStamp() + "j index               : " + j );
 System.out.println(getCurrDateTimeStamp() + "entityRec.ID          : " + entityRec.get_ID() );
 System.out.println(getCurrDateTimeStamp() + "entityRec.Desc        : " + entityRec.get_Desc() );
 System.out.println(getCurrDateTimeStamp() + "entityRec.Type        : " + entityRec.get_Type() );
 System.out.println(getCurrDateTimeStamp() + "entityRec.BusType     : " + entityRec.get_BusType() );
 System.out.println(getCurrDateTimeStamp() + "entityRec.TIN         : " + entityRec.get_TIN() );
 System.out.println(getCurrDateTimeStamp() );


}; // Close while...
于 2013-05-16T08:01:18.113 回答
0

每次在while循环中初始化实体记录即可。

于 2013-05-16T08:33:48.257 回答