Debugging also has a trap?
I think so. I found one today. If I can find it later, continue to add it.
Since the official writing of Java code in April this year, it has become more and more amazed at the magic of Java, not to mention automatic garbage collection. Even her debugging seems to be full of mysteries (however, maybe my java level is too good), actually a function in the circle can get the final return result of the function, the debugger must have similar analysis scripts (I use eclipse 2.1.3 ). however, this powerful feature sometimes helps.
Let's take a look at the following common code:
Import java. util. arraylist;
Import java. util. iterator;
Import java. util. List;
/*
* Iteratordebuggingtrap. Java 2004-7-1
*
*
* Chenxh
*/
/**
* @ Author chenxh
* 2004-7-1
*
*
*/
Public class iteratordebuggingtrap {
Public static void main (string [] ARGs ){
List list = new arraylist ();
List. Add ("");
List. Add ("B ");
List. Add ("C ");
For (iterator iter = List. iterator (); ITER. hasnext ();){
System. Out. println (ITER. Next ());
}
}
}
Execution result
A
B
C
No exception, but if you are
System. Out. println (ITER. Next ());
Add a breakpoint and enclose ITER. Next () for monitoring,
The result is
B
It's strange, isn't it. it is easy to find that the debugger also calls iter. next (): each time a breakpoint is called, the next () of the Child stack is called. The result is related to the number of breakpoints, that is, the number of monitoring times. If you
For (iterator iter = List. iterator (); ITER. hasnext ();){
When a breakpoint is set, the result is
C
If you try again
List. Add ("C ");
Removed, it will unfortunately get a runtime error
Java. util. nosuchelementexception
However, this is nothing to worry about, because I have already understood the cause of the error, but it may be surprising to have encountered such a situation.
Note: The ide I use is eclipse, and my java level is limited. I am not familiar with some mainstream ides, such as JBuilder, I don't know if a tool like jbuider has such a small trap.