【问题标题】:Iterating through a list of items using for-loop [closed]使用for循环遍历项目列表[关闭]
【发布时间】:2019-07-04 12:18:23
【问题描述】:

我尝试遍历列表,但只获得列表的最后一个条目。我想遍历列表,将列表中的每个“利润”项目添加到 current_profit,最后能够返回应计current_profitgetFinanceList() 方法返回一个大小为 2 的列表,如图所示。

这是我的调试日志。它显示了从我的代码给出的方法返回的大小为 2 的列表。使用getFinanceList()

 ((FinanceRepository)this).financeList = {ArrayList@6012}  size = 2
 this = {FinanceRepository@6004} 
 current_expenditure = 0
 current_income = 0
 current_profit = -1000
 farmerApp = {BoxStore@6010} 
 financeBox = {Box@6011} 
 financeList = {ArrayList@6012}  size = 2
0 = {Finance@6025} 
 __boxStore = {BoxStore@6010} 
 category = "farm"
 finance_type = "Income"
 id = 1
 name = "jembe"
 notes = "hdgsbsbsb.."
 payment_type = "Cash"
 profit = {Integer@6032} 500  //this profit
 quantity = "5"
 total_amount = "500"
 total_expenditure = {Integer@6035} 0
 total_income = {Integer@6036} 500
 transaction_date = "17-7-2019"
 user = {ToOne@6038} 
 mCallbacks = null
 shadow$_klass_ = {Class@4418} "class 
com.example.e_farmer.models.Finance"
 shadow$_monitor_ = -2069821990
1 = {Finance@6026} 
 __boxStore = {BoxStore@6010} 
 category = "farm"
 finance_type = "Expenditure"
 id = 2
 name = "spade"
 notes = "bdhxhdd"
 payment_type = "Cash"
 profit = {Integer@6045} -1000 //And this profit
 quantity = "5"
 total_amount = "1000"
 total_expenditure = {Integer@6048} 1000
 total_income = {Integer@6049} 0
 transaction_date = "18-7-2019"
 user = {ToOne@6051} 
 mCallbacks = null
 shadow$_klass_ = {Class@4418} "class 
com.example.e_farmer.models.Finance"
 shadow$_monitor_ = -1997268469
 shadow$_klass_ = {Class@5986} "class 
com.example.e_farmer.repositories.FinanceRepository"
 shadow$_monitor_ = -2070028009
 current_profit = -1000

这就是我的代码。请注意,Finance 类是我的模型类

   public MutableLiveData<Integer> getProfit() {

   //the method getFinanceList returns a financeList of size 2 as  illustrated above in the debug code.

    getFinanceList();

    int current_profit = 0;

    for (int i = 0; i < financeList.size(); i++) {
        Finance finance = financeList.get(i);
        current_profit =+finance.getProfit();
    }

    Log.d(TAG, "getProfit: " + current_profit);

    MutableLiveData<Integer> profit_data = new MutableLiveData<>();
    profit_data.setValue(current_profit);

    return profit_data;
}

我期望 current_profit 获得第一项和第二项的利润并且总和即 500 + (-1000) = - 500 但我只获得最后一项 (-1000)。

【问题讨论】:

  • current_profit += finance.getProfit();试试这个
  • 非常感谢@Lakhwinder Singh。我想我现在应该休息了。今天的编码已经足够了。

标签: java android list for-loop


【解决方案1】:

你写了current_profit =+finance.getProfit(),你的意思是current_profit += finance.getProfit()。您只需分配值,并调用一元 +,它什么也不做。前一个值加值的操作符是+=

【讨论】:

    【解决方案2】:

    改正的方法,问题是=+,改成+=

    public MutableLiveData<Integer> getProfit() {
    
       //the method getFinanceList returns a financeList of size 2 as  illustrated above in the debug code.
    
        getFinanceList();
    
        int current_profit = 0;
    
        for (int i = 0; i < financeList.size(); i++) {
            Finance finance = financeList.get(i);
            current_profit +=finance.getProfit();
        }
    
        Log.d(TAG, "getProfit: " + current_profit);
    
        MutableLiveData<Integer> profit_data = new MutableLiveData<>();
        profit_data.setValue(current_profit);
    
        return profit_data;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 2013-10-15
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多