簡體   English   中英

在兩個部分不同的ArrayList之間傳輸數據

[英]transferring data between two partially different ArrayLists

我有兩個用數據填充的類。 它們的結構相似,並且兩個類都包含一個ArrayList,其結構與另一個類的ArrayList 完全相同

ArrayList稱為t30tB它們的結構是(BigDecimal, String) main中查找new transactionB()new Transaction30() ,它將變得更加清晰。

我想在這些ArrayList之間進行“事務” ,因此,將值從ArrayList( t30 )移動(或復制和刪除) ArrayList( tB

TransactionO:

public class TransactionO{
    public ArrayList<TransactionB>tBpost = new ArrayList<TransactionB>();
    public TransactionO(/*constructor as seen in main...*/){/*stuff*/} ^
                                            //                         |
    public void addBPost(TransactionB b) { //                          |
        this.tBpost.add(b);               //adding ---------------------
    }
}

Transaction00:

public class Transaction00 {
    public ArrayList<Transaction30>t30post = new ArrayList<Transaction30>();
    public Transaction00(/*constructor as shown below*/){/*stuff*/}    ^
                                               //                      |
    public void add30Post(Transaction30 t30) {//                       |
        this.t30post.add(t30);               //adding ------------------
    }
}  

主要:

TransactionO tO = null;
Transaction00 t00 = null;
private static List<TransactionO> allTransactionsIn = new ArrayList<TransactionO>();
private static List<Transaction00> allTransactionsOut = new ArrayList<Transaction00>();
//reading from file, parsing data, and finally storing the data in the class obj
tO = new TransactionO(accountNumber,currency, paymentDate,currentAmount,transactionQty);
tB = new TransactionB(amount,reference); //(BigDecimal, String)
tO.addBPost(tB);
// ^adding the class TransactionB into arraylist "tB" into TransactionO

t00 = new Transaction00(accountNumberSend,clrNrSend);
t30 = new Transaction30(amountSend,referenceSend); //(BigDecimal, String)
t00.add30Post(t30);     
// ^adding the class Transaction30 into arraylist "t30" into Transaction00

//finally
allTransactionsIn.add(tO);
allTransactionsOut.add(t00);

摘要 :在這些^ allTransaction*** ArrayList中,存在不同的類對象,但結構上相同的ArrayList (BigDecimal, String)

arrayList的ASCII結構allTransactionsOut

--------------------------------------------------------
| t00 (constructor-values) | t00.t30 (arrayList-values) | --------------copy
--------------------------------------------------------               |
Structurally different ^   |  Structurally same ^ (BigDecimal, String) |

arrayList的ASCII結構list allTransactionsIn

--------------------------------------------------------               |
| tO (constructor-values) | tO.tB (arrayList-values)    |  <------------
--------------------------------------------------------
Structurally different ^  |  Structurally same ^ (BigDecimal, String)

我如何將這些arrayList-values從一個列表傳輸到另一個列表? (不是constructor-values

請隨時詢問您是否需要進一步的說明。 謝謝

即使TransactionBTransaction30都碰巧具有BigDecimalString類型的字段,Java也不認為它們是相關的。

如果要將這兩種類型都存儲在ArrayList ,則需要確保這兩種事務類型都繼承自同一父類(擴展公共類型或實現公共接口)。

例如

public abstract class ParentTransaction {
     protected BigDecimal value1
     protected String value2

     public ParentTransaction(BigDecimal value1, String value2) {
         this.value1 = value1;
         this.value2 = value2;
     }
}

public class TransactionB extends ParentTransaction {
   public TransactionB(BigDecimal amountSend, String referenceSend) {
       super(amountSend, referenceSend);
   }
   BigDecimal getAmountSend() {
       return value1;
   }
   String getReferenceSend() {
       return value2;
   }
}

public class Transaction30 extends ParentTransaction {
   public TransactionB(BigDecimal account, String reference) {
       super(account, reference);
   }
   BigDecimal getAccount() {
       return value1;
   }
   String getReference() {
       return value2;
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM