簡體   English   中英

在 eclipse 中調試 for 循環的舒適方式

[英]Comfortable way for debugging for-loops in eclipse

如何像在此代碼中一樣調試 eclipse 中的復雜 for 循環?

import java.util.Arrays;
import java.util.List;

public class ListTraining {

    public void countOccurence() {
        List<Integer> numbers = Arrays.asList(3, 4, 3, 5, 2, 3);
        Integer number = 3;     
        for (int i = numbers.indexOf(number), j = -1; i > j; j = i, i += numbers.subList(i + 1, numbers.size()).indexOf(number) + 1) {
            System.out.println("found number: " + number + " at position: " + i);           
        }       
    }

}

如果我使用“Step Over”,我就在指令塊中。 如果我使用“Step Into”,我就在 indexOf 之類的方法中。 如何以舒適的方式調查循環 header 中的變量和調用?

是否可以像條件檢查或變量賦值一樣跳轉到循環的單個指令中?

在 for 循環處暫停調試器。 突出顯示一段代碼。 右鍵單擊它並 select “檢查”選項以查看其值(例如,突出顯示numbers.indexOf(number)以查看 function 的返回值是什么)。

使用調試視圖的變量組件在此處輸入圖像描述

如果您的 for 運行良好,我建議使用 system.println(String the data incremented)。 查看每個循環中的數據。 控制台將顯示循環的內容。

如果你的 for 在某個時候中斷,我建議使用調試模式並在循環中間放置一個斷點,代碼的執行將在每次到達斷點時停止,你可以查看 for 中的所有變量在 state 處循環。

將 for 循環的頭部拆分為多行

for (int i = numbers.indexOf(number), j = -1; 
            i > j; 
            j = i, i += numbers.subList(i + 1, numbers.size()).indexOf(number) + 1) {
        System.out.println("found number: " + number + " at position: " + i);           
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM