我有两个用数据填充的课程。 它们的结构相似,两个类都包含一个ArrayList,它与完全相同的结构与另一个类相同。数组列表。
ArrayLists名为t30
,tB
的结构为(BigDecimal, String)
。查看new transactionB()
和new Transaction30()
的主要,它会变得更加清晰。
我想在这些ArrayLists之间执行"事务" ,因此,移动(或复制和删除)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***
ArrayLists中,有不同的类对象,但结构相同的ArrayLists (BigDecimal, String)
。
arrayList allTransactionsOut
的ASCII结构:
--------------------------------------------------------
| t00 (constructor-values) | t00.t30 (arrayList-values) | --------------copy
-------------------------------------------------------- |
Structurally different ^ | Structurally same ^ (BigDecimal, String) |
arrayList allTransactionsIn
的ASCII结构:
-------------------------------------------------------- |
| tO (constructor-values) | tO.tB (arrayList-values) | <------------
--------------------------------------------------------
Structurally different ^ | Structurally same ^ (BigDecimal, String)
我如何将这些arrayList-values
从一个列表转移到另一个列表? (不是constructor-values
)
请不要犹豫,询问您是否需要进一步解释。感谢
答案 0 :(得分:2)
尽管TransactionB
和Transaction30
碰巧都有BigDecimal
和String
类型的字段,但Java并不认为它们是相关的。< / p>
如果要将这两种类型存储在ArrayList
中,那么您需要确保两种事务类型都从同一父级继承(扩展公共类型或实现公共接口)
e.g。
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;
}
}