Results 1 to 4 of 4
Thread: Enhanced For Statements
- 07-14-2012, 04:12 AM #1
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;
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.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); } } }
On, another note! I discovered that Java has a
function. wow, 6 years of programming, and I am learning so much.Java Code:System.out.printf();

My API:Java Code:cat > a.out || cat > main.class
- 07-14-2012, 10:40 AM #2
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!
- 07-14-2012, 12:48 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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:
orJava Code:for (Iterator<T> i= iterable.iterator(); i.hasNext(); ) { T element= i.next(); ... }
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.Java Code:for (int index= 0; index < array.length; index++) T element= array[index]; ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-15-2012, 12:01 AM #4
Re: Enhanced For Statements
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.
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
-
enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)
By java4amanda in forum New To JavaReplies: 5Last Post: 03-29-2012, 06:22 PM -
enhanced for loop
By billq in forum New To JavaReplies: 1Last Post: 05-01-2010, 02:07 PM -
Enhanced For Loop
By terahawks in forum New To JavaReplies: 3Last Post: 04-16-2010, 08:46 AM -
Help for Enhanced for loop
By jboy in forum New To JavaReplies: 6Last Post: 09-13-2009, 06:45 AM -
Enhanced for loop
By Java Tip in forum Java TipReplies: 0Last Post: 11-03-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks