Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Enhanced For Loop filter_list
Author
Message
Enhanced For Loop #1
hey i can understand the for Loop really well but i never unrstood how the Enhanced For Loop works !!
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);
}

here's and example i know what this code do but i don't know the logic how it works !!

(int item : numbers)i need to know how this work pls !!

@Riverclawz thank you for helping me figure out ho it works on irc i appreciate it !!

pls feel free to close this thread !!
(This post was last modified: 07-23-2014, 11:41 PM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Enhanced For Loop #2
I'm not a Java programmer and someone else will likely be able to answer this much better than I can but here's what looks like is happening.
Code:
int[] numbers =
{1,2,3,4,5,6,7,8,9,10}
This is an array with 10 items in it

Code:
for (int item : numbers) {
System.out.println("Count is: " + item);

for (int item : numbers) {
takes the current item in the array

System.out.println("Count is: " + item);

prints out the item in the array (first item being 1) and adds one to item so the next time the loop runs the second item in the array is printed and so on.

That's how it looks from a general programming perspective anyway.
If you need help feel free to PM me
[Image: klfpJD]
Probitcoin
Freebitcoin
BTC clicks
bitcoin wallet:
1FBPAanbs3rJU9BUpobpDJc9hHUaCaC25N

Reply

RE: Enhanced For Loop #3
true it keeps doing this till the array ends !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Enhanced For Loop #4
That for loop is called a 'for-each loop'

It is is a shorthand notation for writting a loop that acts over an 'iterable collection'

That is to say the object (numbers in this case) need to implement the Iterable interface http://docs.oracle.com/javase/7/docs/api...rable.html

Which just means that it provides a standard interface to looping over all the objects in the collection(array). Basically it provides a nice object that has methods next() hasNext() and remove() for you to loop over. Look at the Iterator interface for more information: http://docs.oracle.com/javase/7/docs/api...rator.html

So you're code behind the scenes would be translated to something like the following:

Code:
Iterator<Int> ite = numbers.iterator();
while(ite.hasNext()) {
   int item = ite.next();
   System.out.println("Count is: " + item);
}

@chmod you're thinking too functionally. for (int item : numbers) might look like a functional construct but its a shorthand notation.
(This post was last modified: 07-24-2014, 02:49 AM by miiike980.)

Reply

RE: Enhanced For Loop #5
Preferably it should be called as For-each loop. These links have good explanations :-
http://www.javatpoint.com/for-each-loop

http://www.leepoint.net/notes-java/flow/...reach.html

http://www.javapractices.com/topic/Topic....do?Id=196
[Image: OilyCostlyEwe.gif]

Reply

RE: Enhanced For Loop #6
Enhanced for loops are used when the programmer wants to iterate through every element within the array. Just thought I'd throw that in there. Smile

Reply

RE: Enhanced For Loop #7
Enhanced for-loops iterate through every element in the specified array unless the loop is stopped (using break or another control flow modification).

Code:
int[] array = {1, 2, 3};

for(int i : array){
   // current iteration
}

In the above example, the enhanced for-loop can execute only three times since the size of the array is 3. The local variable "i" in the enhanced for-loop scope holds the currently iterated element. The elements are iterated in the order of the array. If the enhanced for-loop is on its 2nd iteration, for example, the "i" local variable is equivalent to "2".

Here is a demonstration.
Code:
for each integer in the array, iterating in order, setting the local variable named "i" to the current iterating index value,{
     do this
}

You can use the enhanced for-loop with any class inheriting from "Iterator"; which is how Java Collections work with them.

[+] 1 user Likes Coca.'s post
Reply

RE: Enhanced For Loop #8
(09-09-2015, 11:45 PM)Coca. Wrote: Enhanced for-loops iterate through every element in the specified array unless the loop is stopped (using break or another control flow modification).

Code:
int[] array = {1, 2, 3};

for(int i : array){
   // current iteration
}

In the above example, the enhanced for-loop can execute only three times since the size of the array is 3. The local variable "i" in the enhanced for-loop scope holds the currently iterated element. The elements are iterated in the order of the array. If the enhanced for-loop is on its 2nd iteration, for example, the "i" local variable is equivalent to "2".

Here is a demonstration.
Code:
for each integer in the array, iterating in order, setting the local variable named "i" to the current iterating index value,{
     do this
}

You can use the enhanced for-loop with any class inheriting from "Iterator"; which is how Java Collections work with them.

I'd delete this for grave-digging, but it's quality. Tongue
[Image: fSEZXPs.png]

Reply







Users browsing this thread: 1 Guest(s)