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);