Results 1 to 4 of 4
  1. #1
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Lightbulb Enhanced For Statements

    This is more of a question of opinion then an actual question on my java code. I have been over using the 'for' statement in my programs like teens over use text than any other form of communication. I was doing some research in obscure Java functions, loops and other things and I ran across the Enhanced For Statement.

    I understand how it is used, but I was wondering how many others know about it and how many of you guys actually use it? The Java Tutorial site says to use it [enhanced for statment] more often then normal for statement. Do you guys agree?

    If you do not know what the enhanced for statement is, here is an example;

    Java Code:
    public class efor {
    	public static void main(String args[]) {
    		int[] numbers = 
                 {1,2,3,4,5,6,7,8,9,10};
             for (int item : numbers) {
                 System.out.println("Count is: "+ item);
             }
    	}
    }
    I am still testing this out to see what I can do with it. I found that you can change what type of data you are giving it, for example besides using an int you can use a string, and it still works. I have only tested this out with primitive types.

    On, another note! I discovered that Java has a
    Java Code:
    System.out.printf();
    function. wow, 6 years of programming, and I am learning so much.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Enhanced For Statements

    I only use traditional for statements if I need to do something with the loop index. I use enhanced for statements a lot with collections.
    Get in the habit of using standard Java naming conventions!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Enhanced For Statements

    If one doesn't know what an enhanced for loop is, one doesn't know the entire Java language; note that this loop is just syntactic sugar, i.e. it is transformed to an 'old fashioned' loop by the compiler; the argument on the right is either an Iterable<T> or a simple array; and the compiler knows how to transform such loops:

    Java Code:
    for (Iterator<T> i= iterable.iterator(); i.hasNext(); ) {
       T element= i.next();
       ...
    }
    or

    Java Code:
    for (int index= 0; index < array.length; index++)
       T element= array[index];
       ...
    }
    for Iterables<T> and primitive arrays respectively; note that the Iterator nor the index are available to the programmer which can be a valid reason not to use such a loop.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Enhanced For Statements

    Quote Originally Posted by kjkrum View Post
    Get in the habit of using standard Java naming conventions!
    I have my own Programming naming Convention. I use this a bit, but I like the way i do it. I know that sounds lame, but my convention helps me to program... I might use this if I am in a project.

    Quote Originally Posted by JosAH View Post
    If one doesn't know what an enhanced for a loop is, one doesn't know the entire Java language
    Do I need to know the entire Java Language?
    In my years of programs, I have always felt that learning every nock any cranny of a programming is unnecessary because everything can be learned. Once you understand how programs work, have used what you know in practice, and how machines work with software, then you have mastered how to program and everything else can be easily learned and implemented in the code you right. At least this is my idea... Am I wrong to think this?

    But it is really nice to know this information about enhanced for loops.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. Replies: 5
    Last Post: 03-29-2012, 06:22 PM
  2. enhanced for loop
    By billq in forum New To Java
    Replies: 1
    Last Post: 05-01-2010, 02:07 PM
  3. Enhanced For Loop
    By terahawks in forum New To Java
    Replies: 3
    Last Post: 04-16-2010, 08:46 AM
  4. Help for Enhanced for loop
    By jboy in forum New To Java
    Replies: 6
    Last Post: 09-13-2009, 06:45 AM
  5. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •