Results 1 to 5 of 5

Thread: Which is best for iterator: For loop or while loop

  1. #1
    Join Date
    Dec 2009
    Posts
    62

    Which is best for iterator: For loop or while loop

    The question may appear stupid to most but its an important and major concern for a person like me:

    Code:
    Iterator it = myList.iterator();
    while(it.hasNext()) {
    }
    or:

    Code:
    for(Iterator it = myList.iterator(); it.hasNext(); ) {
    }
    I wrote the second way, but two of my bosses tell me it's wrong, without giving any proper reason. So here arises a big argument for me: which is proper and best to implement an iterator "for" or "while"?

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Which is best for iterator: For loop or while loop

    Well at worst, you can do:

    Code:
        {
         Iterator it = myList.iterator();
         while (it.hasNext()) {
         }
        }
    And suddenly your iterator is of no greater or you use a block with (I think it is not in java?). The argument of the PC, great, even I dare not make them like that. At first glance, I prefer the first syntax, but actually, the second seems much cleaner. Ok, I am speaking more in the Java category

  3. #3
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Which is best for iterator: For loop or while loop

    With C#, I wondered about the absence of the "foreach" in Java (it's still nicer)

    And I came over here: http://java.sun.com/j2se/1.5.0/docs/...e/foreach.html

    Well, not after I master the trick, but it would be better still as syntax? In any case, the article stresses well above. And since this is the official doc.

    Only limited, as in C#, you can not serve yourself to change the number of elements in your list.

    At worst, it will strengthen your position towards the forum, since they do not mention the syntax of the while, which seems out of competition

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Which is best for iterator: For loop or while loop

    Your chief has plenty of free time and probably will seek a lather. As he can not seem to do so by his competence, he does so with arguments that at best I would call harrowing. If it poses a problem of writing in one way or another exchange box, both are completely equivalents and should be read with equal facility by any developer whose intelligence exceeds the searching.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Which is best for iterator: For loop or while loop

    I am of the same opinion as you Leeland: 2nd code reduces the scope of the iterator. Many Internet sites also use this notation.

    Indeed it is perhaps not very "pretty", which is why they invented the foreach, which is implemented in Java since version 1.5 released in 2004. For your example it gives this:

    Code:
    for(Bidule b : myList) {
    }
    In practice, the compiler translates the foreach by your 2nd method, so a loop with an Iterator declared locally, but the syntax is much nicer.

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work
    By mADiRAkSHii in forum Operating Systems
    Replies: 4
    Last Post: 25-01-2011, 07:23 PM
  3. for loop in c
    By poojayadav12 in forum Software Development
    Replies: 1
    Last Post: 11-03-2010, 03:32 PM
  4. How to use For-Each Loop in PHP?
    By Bigga Lexx in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 01:16 AM
  5. Differentiate between Do-While loop and While loop
    By REDBULL in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 10:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,898,406.00455 seconds with 17 queries