Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何计算模型数组列表中的总数?_Java_Android_Model_Pojo - Fatal编程技术网

Java Android:如何计算模型数组列表中的总数?

Java Android:如何计算模型数组列表中的总数?,java,android,model,pojo,Java,Android,Model,Pojo,[抱歉语法不好][如需编辑问题,将不胜感激] 我想创建totalHaveMoney,我试图在totalMoney方法中循环数据income-expense,但失败了。只有从一个数据中获取收入,并且计算不起作用 型号: public class MoneyManager { String name, category; String date, description, key; Long income, expense, totalMoney; public M

[抱歉语法不好][如需编辑问题,将不胜感激]

我想创建
totalHaveMoney
,我试图在
totalMoney
方法中循环数据
income-expense
,但失败了。只有从一个数据中获取
收入
,并且计算不起作用

型号:

public class MoneyManager {
    String name, category;
    String date, description, key;
    Long income, expense, totalMoney;

    public MoneyManager(String name, String category, String date, Long income,Long expense , String description) {
        this.name = name;
        this.category = category;
        this.date = date;
        this.income = income;
        this.expense = expense;
        this.description = description;
    }

    public MoneyManager() {
    }
    public Long totalMoney(){
        Long haveMoney = getIncome();

        for (int i = 0; i < this.getIncome(); i++){
            haveMoney -= getExpense();
        }
        return haveMoney;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Long getExpense() {
        return expense;
    }

    public void setExpense(Long expense) {
        this.expense = expense;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public Long getIncome() {
        return income;
    }

    public void setIncome(Long income) {
        this.income = income;
    }

    public Long getTotalMoney() {
        return totalMoney;
    }

    public void setTotalMoney(Long totalMoney) {
        this.totalMoney = totalMoney;
    }
}
money.totalMoney()
的结果与
收入相同,不计算



如果您有一个
列表
,您可以反复浏览并添加

long sum = 0;
for (MoneyManager mm : listOfMoneyManager) { // or whatever your local variable is
    sum += mm.getIncome() - mm.getExpense();
}

// after the iteration you have the total

System.out.println (sum);

我在你的代码中看不到任何
列表
数组
-请说明你的意思我只想计算
收入-费用
,但我总是失败你为什么循环?为什么不
返回getIncome()-getExpense()-无需循环。我想获取所有arraylist数据并计算它们。但我不知道,因为我在上面的文章中只计算单个数据,正如我之前所说的,我在代码中看不到任何列表或数组。您是否有
MoneyManager
对象的
列表?
long sum = 0;
for (MoneyManager mm : listOfMoneyManager) { // or whatever your local variable is
    sum += mm.getIncome() - mm.getExpense();
}

// after the iteration you have the total

System.out.println (sum);