我需要幫助,瞭解如何使用for循環設置值,而不必在使用類時替換現有值。例如,我需要輸出三個不同的值,分別是test1,test2,test3。下面是代碼,當使用相同的類時,使用for循環設置一個值而不替換現有的值
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);
電流輸出
<PmtInf>
<Dbtr>
<Nm>test3</Nm>
</Dbtr>
</PmtInf>
預期輸出
<PmtInf>
<Dbtr>
<Nm>test1</Nm>
<Nm>test2</Nm>
<Nm>test3</Nm>
</Dbtr>
</PmtInf>
實現類
protected String nm;
public String getNm() {
return nm;
}
public void setNm(String value) {
this.nm = value;
}
謝謝你的幫助
刪除條件。 – Omore
請把setNm()實現代碼 –
使用if else語句的原因是由於位置。該值必須與列表中的位置完全相同。例如(test1,test3), –