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.
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:
Code:
for (Iterator<T> i= iterable.iterator(); i.hasNext(); ) {
T element= i.next();
...
}
or
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
Re: Enhanced For Statements
Quote:
Originally Posted by
kjkrum
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
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.