Java 如何在数组列表中执行计算?
我有两个数组列表。 一个名为FinalAmount的数组列表 其中包含以下列:名称、帐户号、帐户类型、金额、最终日期Java 如何在数组列表中执行计算?,java,arrays,arraylist,Java,Arrays,Arraylist,我有两个数组列表。 一个名为FinalAmount的数组列表 其中包含以下列:名称、帐户号、帐户类型、金额、最终日期 Example: Peter,10025,Savings,50000,20150408 Stuart,10054,Savings,21000,20150608 名为事务的第二个数组列表 其中包含以下列:名称、帐户号、TransacticeDamount、日期 Example: Peter,10025,5000,20150404 Stuar
Example: Peter,10025,Savings,50000,20150408
Stuart,10054,Savings,21000,20150608
名为事务的第二个数组列表
其中包含以下列:名称、帐户号、TransacticeDamount、日期
Example: Peter,10025,5000,20150404
Stuart,10054,1000,20150406
我想从金额(FinalAmount数组列表)中减去最终日期下的TransactiveDamount(事务数组列表)。并从事务数组列表中删除减法后的行
因此,FinalAmount数组列表的输出是
Sample Output: Peter,10025,Savings,45000,20150408
Stuart,10054,Savings,20000,20150608
并且事务数组列表中的两行都将被删除
如果您有想法,请分享您的想法或代码。这是示例答案。当我检查您的问题时,我发现我需要创建一个人类,名称、accountNo、amount、accType、Date。当我们检查要减去的两个数组列表时,我们需要在两个数组中找到相同的AccountNo。因此,我们需要使用contains方法,并重写equal和hashCode。我的示例Person类如下所示: 包java7.demo
import java.util.Date;
public class Person {
private String name;
private int accountNo;
private String accType;
private int amount;
private Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
public String getAccType() {
return accType;
}
public void setAccType(String accType) {
this.accType = accType;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + accountNo;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (accountNo != other.accountNo)
return false;
return true;
}
@Override
public String toString() {
return "name=" + name + ", accountNo=" + accountNo
+ ", accType=" + accType + ", amount=" + amount + ", date="
+ date + "";
}
}
主类就像:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AccountSubtractionTest {
@SuppressWarnings("deprecation")
public static void main(String args[]){
List<Person>FinalAmount = new ArrayList<Person>();
List<Person>Transcation = new ArrayList<Person>();
@SuppressWarnings("deprecation")
Date d1 = new Date("01/01/2016");
@SuppressWarnings("unused")
Date d2 = new Date("04/01/2016");
Person p1 = new Person();
p1.setName("Peter");
p1.setAccountNo(1234);
p1.setAccType("Saving");
p1.setAmount(5000);
p1.setDate(d1);
Person p2 = new Person();
p2.setName("Robin");
p2.setAccountNo(2222);
p2.setAccType("Saving");
p2.setAmount(5000);
p2.setDate(new Date("02/02/2016"));
FinalAmount.add(p1);
FinalAmount.add(p2);
Person p3 = new Person();
p3.setName("Peter");
p3.setAccountNo(1234);
p3.setAccType("Saving");
p3.setAmount(1000);
p3.setDate(d2);
Person p4 = new Person();
p4.setName("Robin");
p4.setAccountNo(2222);
p4.setAccType("Saving");
p4.setAmount(2000);
p4.setDate(new Date("04/02/2016"));
Transcation.add(p3);
Transcation.add(p4);
for(Person p:FinalAmount){
if(Transcation.contains(p)){
int index = Transcation.indexOf(p);
Person person = Transcation.get(index);
int amount = p.getAmount() - person.getAmount();
p.setAmount(amount);
Transcation.remove(index);
}
}
for(int i=0; i<FinalAmount.size(); i++){
System.out.println(FinalAmount.get(i));
}
}
}
name=Peter, accountNo=1234, accType=Saving, amount=4000, date=Fri Jan 01 00:00:00 MMT 2016
name=Robin, accountNo=2222, accType=Saving, amount=3000, date=Tue Feb 02 00:00:00 MMT 2016
请运行我提供的两个类作为示例,并查看您需要修改的代码。并检查这两个类,然后告诉您是否真的想修改它???@holidayCoder感谢链接&是的,我真的想这样做。因为您的
列表既包含整数又包含字符串,因此它很复杂,但您可以尝试映射相反,如果您只想计算这3个值