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

Java 在两个部分不同的ArrayList之间传输数据,java,arraylist,Java,Arraylist,我用数据填充了两个类。 它们都具有相似的结构,并且两个类都包含一个与另一个类的ArrayList结构完全相同的ArrayList 数组列表被称为t30和tB它们的结构是(BigDecimal,String)。在main中查找newtransactionb()和newtransaction30(),它会变得更加清晰 我想在这些ArrayList之间执行一个“事务”,因此,将值从ArrayList(t30)移动(或复制和删除)到ArrayList(tB) 交易: public class Trans

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

数组列表被称为
t30
tB
它们的结构是
(BigDecimal,String)
。在main中查找
newtransactionb()
newtransaction30()
,它会变得更加清晰

我想在这些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 ---------------------
    }
}
arrayList的ASCII结构
AllTransactionIn

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

|tO(构造函数值)| tO.tB(arrayList值)|即使
TransactionB
Transaction30
碰巧都有类型为
BigDecimal
String
的字段,Java认为它们没有关系

如果要将这两种类型存储在
ArrayList
中,则需要确保这两种事务类型都继承自同一父级(扩展公共类型或实现公共接口)

e、 g


这读起来很混乱。你说的
ArrayList(BigDecimal,String)
是什么意思?@AndyTurner当然,这不起作用,因为我的ArrayList的“构造函数部分”是不同的。我认为你应该创建一个方法,将值从一个数组逐个复制到另一个数组。你考虑过使用
接口来实现所有这些结构上的相似性吗?多态性这不是MCVE。它不是最小的,因为它非常混乱(例如,名称
O
00
B
,和
30
——你能让代码读起来不像绕口令吗?),它不完整,因为
数量
没有定义。
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);
--------------------------------------------------------
| t00 (constructor-values) | t00.t30 (arrayList-values) | --------------copy
--------------------------------------------------------               |
Structurally different ^   |  Structurally same ^ (BigDecimal, String) |
--------------------------------------------------------               |
| tO (constructor-values) | tO.tB (arrayList-values)    |  <------------
--------------------------------------------------------
Structurally different ^  |  Structurally same ^ (BigDecimal, String)
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;
   }
}