【问题标题】:transferring data between two partially different ArrayLists在两个部分不同的 ArrayList 之间传输数据
【发布时间】:2018-04-30 22:03:00
【问题描述】:

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

ArrayList 被称为t30tB,它们的结构是(BigDecimal, String)。在 ma​​in 中查找new transactionB()new Transaction30(),它会变得更加清晰。

我想在这些 ArrayList 之间进行 “事务”,因此,将 ArrayList (t30) 中的值移动(或复制和删除) ArrayList (@987654327 @)

交易O:

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)

arrayList的ASCII结构allTransactionsOut:

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

arrayList的ASCII结构allTransactionsIn:

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

我如何将这些arrayList-values 从一个列表转移到另一个列表? (不是constructor-values

如果您需要进一步的解释,请随时询问。谢谢

【问题讨论】:

  • 这读起来很混乱。 ArrayList(BigDecimal, String) 是什么意思?
  • @AndyTurner 当然,这不起作用,因为我的 ArrayLists 的“构造函数部分”不同。
  • 我认为您应该创建一种方法来将值从一个数组一个一个复制到另一个
  • 您是否考虑过使用interface 来实现所有这些结构相似性?多态性
  • 这不是 MCVE。从某种意义上说,它不是最小的,它非常令人困惑(例如,名称O00B30——你能让代码读起来不像绕口令吗?),而且它是未完成,因为未定义 amount

标签: java arraylist


【解决方案1】:

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

【讨论】: