如何在数组列表中执行计算?

时间:2016-06-08 03:35:37

标签: java arrays arraylist

我有两个数组列表。 一个名为 FinalAmount 的数组列表。

其中包含以下列:Name,AccountNo,AccntType,Amount,FinalDate

Example: Peter,10025,Savings,50000,20150408

         Stuart,10054,Savings,21000,20150608

名为交易的第二个数组列表。

其中包含以下列:Name,AccntNo,transactedAmount,Date

Example: Peter,10025,5000,20150404

         Stuart,10054,1000,20150406

我想从Amount(FinalAmount数组列表)中减去最终日期下的transactedAmount(Transactions Array列表)。从Transactions数据列表中减去后删除该行。

因此 FinalAmount 数组列表的输出为

Sample Output: Peter,10025,Savings,45000,20150408

               Stuart,10054,Savings,20000,20150608

交易数组列表中的所有行都将被删除。

如果您有一个方法,请分享您的想法或代码。

1 个答案:

答案 0 :(得分:1)

这是示例答案。当我检查你的问题时,我看到我需要创建一个名为accountNo,amount,accType,Date的人类。当我们检查两个数组List要减去时,我们需要找到相同的两个数组中的AccountNo。所以我们需要使用contains方法,并且我重写了equal和hashCode.My Sample Person类就像: package 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));
        }






    }

}

在主类中,我只使用日期d1 =新日期(“01/01/2016”)。所以你需要改变你想要使用的日期类型格式。我的日期只是为了样本。当我跑代码输出get就像:

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

请运行我提供的两个类作为示例,请查看需要修改的代码。