在两个部分不同的 ArrayList 之间传输数据



我有两个类,我用数据填充了它们。 它们的结构相似,并且两个类都包含一个与另一个类的 ArrayList 具有完全相同结构的 ArrayList。

数组列表称为t30tB其结构(BigDecimal, String) 。在主要查找new transactionB()new Transaction30(),它会变得更加清晰。

我想在这些 ArrayList 之间进行">事务">,因此,将值从 ArrayList ( t30 ( 移动到 ArrayList ( tB (

交易对象:

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 ---------------------
    }
}

事务00:

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*** ArrayLists中,有不同的类对象,但结构上相同的ArrayLists (BigDecimal, String)

数组列表allTransactionsOut的 ASCII 结构:

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

数组列表的 ASCII 结构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;
   }
}

最新更新