+ 19

[SOLVED] What is this t: primes doing in for(int t:primes) and why is it traversing through the array??

Pls explain t:primes in the for loop.....

27th Jul 2020, 7:57 AM
Alphin K Sajan
Alphin K Sajan - avatar
5 Respostas
+ 5
In Java this colon ':' character means "for each".In int t : primes. t stores the next element of the primes array if it exists each times the loop runs.
27th Jul 2020, 8:57 AM
Akash Sharma
Akash Sharma - avatar
+ 11
Thnk uu
27th Jul 2020, 9:08 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 9
Java? It means -> for each `int` variable as <t> in <primes>.
27th Jul 2020, 8:01 AM
Ipang
+ 8
That is short form version of for loop used to traverse array.. It's don't require to mention array length.. int primes[] ={2,3,5,7,11,13}; for(int i : primes) System.out.println(i); Its is used for iterables like Arrays, strings, lists... Edit: in java, pls mention or tag the language
27th Jul 2020, 8:16 AM
Jayakrishna 🇮🇳
+ 3
Alphin Sajan You're Always Welcome bro.
27th Jul 2020, 3:16 PM
Akash Sharma
Akash Sharma - avatar