Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: For vs. For Each loops?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default For vs. For Each loops?

    I am a bit new to for each loops and I'm trying to understand what are the main differences between them when working with arrays. Is there any particular set where the for each is more convenient? Is the for each basically saying for (int i = 0; i <= array.length(); i++) { array[0]} ???

    Thanks.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: For vs. For Each loops?

    Let's say you have an array of Strings:

    String[] animals = {"cats", "dogs", "lizards"};

    Then this:

    for(String s : animals){
       System.out.println(s);
    }

    ...is equivalent to this:

    for(int i = 0; i < animals.length; i++){
       String s = animals[i];
       System.out.println(s);
    }

    It's a little more complicated than that for collections, but for arrays that's pretty much all that's happening. You should choose whichever makes more sense to you, there really isn't a huge benefit of one over the other, except for a few keystrokes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: For vs. For Each loops?

    One difference is when using a for each with a collection. The "hidden code" uses an iterator which can cause problems if the contents of the collection are changed in the loop.

    When deleting elements from an arraylist I like to start at the last element and move towards the first element so there are no changes in the indexes to the elements to be looked at next.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: For vs. For Each loops?

    Quote Originally Posted by Norm View Post
    One difference is when using a for each with a collection. The "hidden code" uses an iterator which can cause problems if the contents of the collection are changed in the loop.

    When deleting elements from an arraylist I like to start at the last element and move towards the first element so there are no changes in the indexes to the elements to be looked at next.
    So I will always need to implement the iterator interface if I am making my own collection?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: For vs. For Each loops?

    need to implement the iterator interface if I am making my own collection?
    What do you mean by "making your own collection"? Are you writing a class that implements Collection? It has a lot of methods that you will have to implement.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: For vs. For Each loops?

    Quote Originally Posted by Norm View Post
    What do you mean by "making your own collection"? Are you writing a class that implements Collection? It has a lot of methods that you will have to implement.
    In my class the professor was showing us a "Bag" program that he was writing and it was a collection. He told us that a way to be able to iterate the Bag program was to put a subclass called BagIterator which implements Iterator and later he was able to use a for each loop to iterate the specific class where he was declaring all the methods in "Bag".

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: For vs. For Each loops?

    I was talking about the java interface: Collection
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 12:54 PM
  2. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 06:43 PM
  3. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2011, 12:42 AM
  4. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 01:38 PM
  5. Replies: 11
    Last Post: April 29th, 2009, 04:12 AM