2017-04-05 18 views
0

我需要幫助,瞭解如何使用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; 
     } 

謝謝你的幫助

+0

刪除條件。 – Omore

+0

請把setNm()實現代碼 –

+0

使用if else語句的原因是由於位置。該值必須與列表中的位置完全相同。例如(test1,test3), –

回答

0

試着把這行:

totalLst.add(paymentInstructionInformation3); 

成環。這導致totalList已將3 paymentInstructionInformation3添加到for循環中。

如果沒有幫助請寫下會發生什麼

+0

它會打印三次也是,我所期望的只是需要打印出三次。BTW,謝謝你的幫助。 –

0

爲什麼你需要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) ; 
} 
0

請將paymentInstructionInformation3對象創建語句中的for循環第一個語句。這將避免重寫值。將for循環中的totalLst.add()語句放置爲最後一個語句,以便將所有三個項目添加到列表中。

0

此行TotalLst.add(paymentInstructionInformation3);不在環路中。所以,它只添加一次而不是第三次。

0

你可以做兩件事情:1。 你可以把初始化和 實例paymentInstructionInformation3 進入循環前的if/else塊和行 totalLst.add(paymentInstructionInformation3); 此塊之後。但在這種情況下,您將有3個PaymentInstructionInformation3實例。 2.您可以保留類PaymentInstructionInformation3的成員,該成員稱爲我猜nm不是作爲單個實體,而是作爲此實體的列表。並且在每個if/else部分中,您都可以爲此列表添加值。

+0

你說什麼我需要什麼,nm是一個實體列表,所以當每次循環時我怎麼能保持類的成員? –

+0

@HolicGoh我的意思是做不服這樣 'code' 類PartyIdentification32 { //可以保持納米以這種方式 列表 NM =新的ArrayList <>(); //所以你需要重構此方法 public void setNm(String nmValue) { nm.add(nmValue); } } 'code' –

0

首先,你爲什麼使用一個循環來創建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。setDbtrpaymentInstructionInformation3.addDbtr並將該參數添加到PaymentInstructionInformation3中的列表中。例如:

class PaymentInstructionInformation3 { 
    List<PartyIdentification32> dbtrList = new ArrayList<>(); 

    public void addDbtr(PartyIdentification32 debtr) { 
     this.dbtrList.add(debtr); 
    } 
} 

我希望它有幫助。

相關問題