简体   繁体   English

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

[英]transferring data between two partially different ArrayLists

I have two classes that I've populated with data. 我有两个用数据填充的类。 They are both similarly structured and both classes contain an ArrayList that have the exact same structure as the other class' ArrayList. 它们的结构相似,并且两个类都包含一个ArrayList,其结构与另一个类的ArrayList 完全相同

The ArrayLists are called t30 and tB their structure is (BigDecimal, String) . ArrayList称为t30tB它们的结构是(BigDecimal, String) Look in main for new transactionB() and new Transaction30() and it'll become more clear. main中查找new transactionB()new Transaction30() ,它将变得更加清晰。

I want to do a "transaction" between these ArrayLists, so, moving (or copying&deleting) the values from ArrayList ( t30 ) to ArrayList ( tB ) 我想在这些ArrayList之间进行“事务” ,因此,将值从ArrayList( t30 )移动(或复制和删除) ArrayList( tB

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

Main: 主要:

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);

Summary : In these ^ allTransaction*** ArrayLists there are different class-objects but structurally-identical ArrayLists (BigDecimal, String) . 摘要 :在这些^ allTransaction*** ArrayList中,存在不同的类对象,但结构上相同的ArrayList (BigDecimal, String)

ASCII structure of arrayList allTransactionsOut : arrayList的ASCII结构allTransactionsOut

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

ASCII structure of arrayList allTransactionsIn : arrayList的ASCII结构list allTransactionsIn

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

How would i transfer these arrayList-values from one list to another? 我如何将这些arrayList-values从一个列表传输到另一个列表? (NOT constructor-values ) (不是constructor-values

Please don't hesitate to ask if you need further explanation. 请随时询问您是否需要进一步的说明。 Thanks 谢谢

Even though TransactionB and Transaction30 both happen to have fields of type BigDecimal and String , Java doesn't think they're related. 即使TransactionBTransaction30都碰巧具有BigDecimalString类型的字段,Java也不认为它们是相关的。

If you want to store both types in an ArrayList , then you'll need to make sure that both transaction types inherit from the same parent (either extend a common type, or implement a common interface). 如果要将这两种类型都存储在ArrayList ,则需要确保这两种事务类型都继承自同一父类(扩展公共类型或实现公共接口)。

eg 例如

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