简体   繁体   English

在 eclipse 中调试 for 循环的舒适方式

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

How can I debug complicated for-loops in eclipse like in this code?如何像在此代码中一样调试 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);           
        }       
    }

}

If I use "Step Over" I'm in the instruction block.如果我使用“Step Over”,我就在指令块中。 If I use "Step Into" I'm inside methods like indexOf.如果我使用“Step Into”,我就在 indexOf 之类的方法中。 How can I investigate the variables and calls in the loop header in a comfortable way?如何以舒适的方式调查循环 header 中的变量和调用?

Is it possible to jump in the single instructions of the loop like in the condition check or in the variable assignment?是否可以像条件检查或变量赋值一样跳转到循环的单个指令中?

Pause the debugger at the for loop.在 for 循环处暂停调试器。 Highlight a section of code.突出显示一段代码。 Right click it and select the "Inspect" option to see it's value (eg, highlight numbers.indexOf(number) to see what the return value of that function is).右键单击它并 select “检查”选项以查看其值(例如,突出显示numbers.indexOf(number)以查看 function 的返回值是什么)。

use debug view's variables components使用调试视图的变量组件在此处输入图像描述

If your for is running fine, i suggest using system.println(String the data incremented).如果您的 for 运行良好,我建议使用 system.println(String the data incremented)。 To see the data in each loop.查看每个循环中的数据。 The console will show the content of the loop.控制台将显示循环的内容。

If your for is breaking at some point, i suggest using debug mode and putting a break point in the middle of the loop, the execution of the code will stop each time it reaches the breakpoint and you can look at all the variables in the for loop at that state.如果你的 for 在某个时候中断,我建议使用调试模式并在循环中间放置一个断点,代码的执行将在每次到达断点时停止,你可以查看 for 中的所有变量在 state 处循环。

Split the head of the for loop into multiple lines将 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