Eclipse debugging "trap"

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.