我有点新,我正在做一个项目。我有一个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 || entityRec.Type: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 index 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():Not For Profit
2013-05-16 03:40:53:042 || structRec.get(j).get_TIN():987654321
2013-05-16 03:40:53:042 || structRec index 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():Not For Profit
2013-05-16 03:40:53:058 || structRec.get(j).get_TIN():987654321
答案 0 :(得分: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...
答案 1 :(得分:0)
每次在while循环中初始化实体记录。