Geeks Talk

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.

enhance for loop in jdk1.5

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

Go Back   Geeks Talk > Software Development > Java
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

Java Java related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2008
Junior Member
 
Join Date: May 2008
Location: india
Posts: 10
Thanks: 1
Thanked 1 Time in 1 Post
anveth is on a distinguished road
enhance for loop in jdk1.5

hii all,
can anybody explain enhance for loop(jdk1.5) with an example .
Thanks
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-26-2008
Junior Member
 
Join Date: May 2008
Location: india
Posts: 10
Thanks: 1
Thanked 1 Time in 1 Post
anveth is on a distinguished road
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
Reply With Quote
  #3 (permalink)  
Old 06-24-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 989
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
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
Reply With Quote
Reply

  Geeks Talk > Software Development > Java

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 08:43 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved