简体   繁体   English

调试循环

[英]Debugging loops

For some current projects, I'm working with several data structures that are pretty large (in the area of 10K elements). 对于一些当前的项目,我正在使用几个非常大的数据结构(在10K元素的区域内)。 To be able to access this data in lists, I need to use loops and iterators, which can be a pain when the problem area is in the latter half of the list. 为了能够在列表中访问这些数据,我需要使用循环和迭代器,当问题区域位于列表的后半部分时,这可能会很麻烦。

So I find myself spending alot of time with my finger on the F8 button in Eclipse's debugger to loop through each element of an iterating loop. 因此,我发现自己花了很多时间在Eclipse的调试器中的F8按钮上循环遍历迭代循环的每个元素。 This gets worse when have to step through that particular section several times to get an idea why the code is reacting a particular way. 当必须多次单步执行该特定部分以了解代码为何以特定方式作出反应时,情况会变得更糟。

If one has a general idea how many times a loop is to execute before a problem area is hit, is there a way to set a loop breakpoint to execute up to that point then pause? 如果一个人知道在遇到问题区域之前要执行多少次循环,有没有办法设置一个循环断点来执行到那个点然后暂停?

I believe there's a better way to do this, but you can create a trivial block of code in the loop that only executes at a certain iteration, and put the breakpoint inside of it. 我相信有更好的方法可以做到这一点,但你可以在循环中创建一个简单的代码块,只在某个迭代中执行,并将断点放在其中。

if (loopIndex == 1000) {
  int number = 14;            //Break here
}

Using this as an example: 以此为例:

for(int i=0;i<10000;i++){
    System.out.println(i);
}

Set a breakpoint on the print line, then right click on it and select Breakpoint Properties... . 在打印行上设置断点,然后右键单击它并选择Breakpoint Properties... From here you can set a condition to trigger the breakpoint. 从这里您可以设置触发断点的条件。 This is the similar to a conditional you would have in an if-statement. 这类似于if语句中的条件。 If you wanted to trigger the breakpoint when i equals 6000, check the Conditional box and try this: 如果你想在i等于6000时触发断点,请检查Conditional框并尝试: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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