简体   繁体   中英

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.

The ArrayLists are called t30 and tB their structure is (BigDecimal, String) . Look in main for new transactionB() and new Transaction30() and it'll become more clear.

I want to do a "transaction" between these ArrayLists, so, moving (or copying&deleting) the values from ArrayList ( t30 ) to 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 ------------------
    }
}  

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

ASCII structure of arrayList allTransactionsOut :

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

ASCII structure of arrayList 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? (NOT 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM