繁体   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