Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on enhance for loop in jdk1.5 within the Java forums, part of the Software Development category; hii all, can anybody explain enhance for loop(jdk1.5) with an example . Thanks...
|
|||||||
|
|||
|
Re: enhance for loop in jdk1.5
in java1.4 version, for retriving data in collections we use while loop as
//Iterator itr=component.iterator(); while(itr.next()) but not for loop because it takes 3 steps for execution as for(initalitation/*step1*/;condition/*step2*/;iteration/*step3*/) for this drawback, jdk1.5 introduced enhance for loop as shown below for(condition) { //staement } But i can't know how to use this for loop |
|
|||
|
Re: enhance for loop in jdk1.5
Hi,
Enhance for loop is given below........... Consider the following method, which takes a collection of timer tasks and cancels them: void cancelAll(Collection<TimerTask> c) { for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); ) i.next().cancel(); } The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong. The for-each construct gets rid of the clutter and the opportunity for error. Here is how the example looks with the for-each construct: void cancelAll(Collection<TimerTask> c) { for (TimerTask t : c) t.cancel(); } The for-each construct is also applicable to arrays, where it hides the index variable rather than the iterator Thanks, Riju |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Difference between JDK1.4.2, JDK 1.5.0 and JDK1.6 | Geek_Guest | Java | 9 | 06-23-2009 05:59 AM |
| difference between jdk1.4 and jdk1.5 | purushothamandk | Java | 3 | 07-06-2008 08:21 AM |
| Strategies You Can Use to Enhance Your Vocabulary | Lokesh M | Personality Development | 3 | 06-16-2008 05:56 AM |
| classes in jdk1.5 | ranjan2k7 | Java | 1 | 01-06-2008 04:59 AM |