Java 在使用同一类时,使用for循环设置值而不替换现有值
我需要关于如何在使用类时使用for循环设置值而不替换现有值的帮助。例如,我需要打印出三个不同的值,分别是test1、test2、test3。下面是代码Java 在使用同一类时,使用for循环设置值而不替换现有值,java,xml,for-loop,Java,Xml,For Loop,我需要关于如何在使用类时使用for循环设置值而不替换现有值的帮助。例如,我需要打印出三个不同的值,分别是test1、test2、test3。下面是代码 PaymentInstructionInformation3 paymentInstructionInformation3 = new PaymentInstructionInformation3(); for(int i=0 ;i<4;i++){ if(i==1) {
PaymentInstructionInformation3 paymentInstructionInformation3 = new PaymentInstructionInformation3();
for(int i=0 ;i<4;i++){
if(i==1)
{
PartyIdentification32 partyIdentification32Body1 = new PartyIdentification32();
partyIdentification32Body1.setNm("test1");
paymentInstructionInformation3.setDbtr(partyIdentification32Body1);
}else
if(i==2)
{
PartyIdentification32 partyIdentification32Body2 = new PartyIdentification32();
partyIdentification32Body2.setNm("test2");
paymentInstructionInformation3.setDbtr(partyIdentification32Body2);
}else
if(i==3)
{
PartyIdentification32 partyIdentification32Body3 = new PartyIdentification32();
partyIdentification32Body3.setNm("test3");
paymentInstructionInformation3.setDbtr(partyIdentification32Body3);
}
}
totalLst.add(paymentInstructionInformation3);
感谢您的帮助试着把这句话说出来:
totalLst.add(paymentInstructionInformation3);
进入for循环。这导致totalist
在for循环中添加了3paymentInstructionInformation3
如果没有帮助,请写下发生了什么为什么需要If语句?你在每个街区都做着完全相同的事情 此外,必须在循环内添加到列表中,而不是在循环结束后添加
for(int i=1 ;i<4;i++){
PaymentInstructionInformation3 pay = new PaymentInstructionInformation3();
PartyIdentification32 party = new PartyIdentification32();
party.setNm("test"+i);
pay.setDbtr(party);
totalLst.add(pay) ;
}
for(int i=1;i请将paymentInstructionInformation3对象创建语句作为第一条语句放在for循环中。这将避免重写值。将totalst.add()语句作为最后一条语句放在for循环中,以便将所有三项添加到列表中。此行totalst.add(PaymentInstructionInformation 3);不在循环中。因此,它只需添加一次,而不是第三次。您可以做两件事:
1.您可以将初始化和实例化
付款说明信息3
在if/else块和行之前进入循环
总计添加(付款说明信息3);
但在本例中,您将有3个PaymentInstructionInformation3实例。
2.您可以保留PaymentInstructionInformation 3类的成员,该类信息称nm不是作为单个实体,而是作为该实体的列表。在每个if/else部分中,您都可以为该列表添加值。首先,为什么要使用循环创建partyIdentification?因为您总是创建partyIdentification32BodyN,所以您可以安装在没有循环的情况下对其进行提示。它将保持如下状态:
PaymentInstructionInformation3 paymentInstructionInformation3 = new PaymentInstructionInformation3();
PartyIdentification32 partyIdentification32Body1 = new PartyIdentification32();
partyIdentification32Body1.setNm("test1");
paymentInstructionInformation3.setDbtr(partyIdentification32Body1);
PartyIdentification32 partyIdentification32Body2 = new PartyIdentification32();
partyIdentification32Body2.setNm("test2");
paymentInstructionInformation3.setDbtr(partyIdentification32Body2);
PartyIdentification32 partyIdentification32Body3 = new PartyIdentification32();
partyIdentification32Body3.setNm("test3");
paymentInstructionInformation3.setDbtr(partyIdentification32Body3);
其次,您的paymentInstructionInformation3.setDbtr似乎没有将partyIdentification32BodyN添加到列表中,以替换前一个值
您可以将paymentInstructionInformation3.setDbtr更改为paymentInstructionInformation3.addDbtr,并将参数添加到paymentInstructionInformation3内的列表中。示例:
class PaymentInstructionInformation3 {
List<PartyIdentification32> dbtrList = new ArrayList<>();
public void addDbtr(PartyIdentification32 debtr) {
this.dbtrList.add(debtr);
}
}
class Payment说明信息3{
List dbtrList=new ArrayList();
公共无效添加DBTR(PartyIdentification32债务人){
this.dbtrList.add(债务人);
}
}
我希望它有帮助。删除if条件。请放入setNm()实现代码使用if-else语句的原因是位置。该值必须与列表中的位置完全相同。例如(test1,test3),你所说的正是我所需要的,nm是一个实体列表,那么每次循环时我如何保持类的成员?@HolicGoh我的意思是这样做code
class PartyIdentification32{//你可以这样保持nm list nm=new arrarylist();//所以你需要重构这个方法public void setNm(字符串nmValue){nm.add(nmValue);}}code
它也会打印三次,我期望的是只需要打印三次。顺便说一句,谢谢你的帮助。
for(int i=1 ;i<4;i++){
PaymentInstructionInformation3 pay = new PaymentInstructionInformation3();
PartyIdentification32 party = new PartyIdentification32();
party.setNm("test"+i);
pay.setDbtr(party);
totalLst.add(pay) ;
}
PaymentInstructionInformation3 paymentInstructionInformation3 = new PaymentInstructionInformation3();
PartyIdentification32 partyIdentification32Body1 = new PartyIdentification32();
partyIdentification32Body1.setNm("test1");
paymentInstructionInformation3.setDbtr(partyIdentification32Body1);
PartyIdentification32 partyIdentification32Body2 = new PartyIdentification32();
partyIdentification32Body2.setNm("test2");
paymentInstructionInformation3.setDbtr(partyIdentification32Body2);
PartyIdentification32 partyIdentification32Body3 = new PartyIdentification32();
partyIdentification32Body3.setNm("test3");
paymentInstructionInformation3.setDbtr(partyIdentification32Body3);
class PaymentInstructionInformation3 {
List<PartyIdentification32> dbtrList = new ArrayList<>();
public void addDbtr(PartyIdentification32 debtr) {
this.dbtrList.add(debtr);
}
}