简体   繁体   中英

Comfortable way for debugging for-loops in eclipse

How can I debug complicated for-loops in eclipse like in this code?

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. If I use "Step Into" I'm inside methods like indexOf. How can I investigate the variables and calls in the loop header in a comfortable way?

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

use debug view's variables components在此处输入图像描述

If your for is running fine, i suggest using 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.

Split the head of the for loop into multiple lines

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM