Forums Register Login

for loop

+Pie Number of slices to send: Send
Hi,

Can You Explain,how this loop works



Animal [] a = {new Animal(), new Dog(), new Animal()};
for(Animal animal : a)
{
//stmts
}
+Pie Number of slices to send: Send
 

Originally posted by lakshmi amulya Hi,

Can You Explain,how this loop works



Animal [] a = {new Animal(), new Dog(), new Animal()};
for(Animal animal : a)
{
//stmts
}



When your enhanced for-loop iterate first, then animal will refer to a[0] like this animal=a[0];

your for loop is similar to this...

for(int i=0;i < a.length; i++){
Animal animal=a[i];
// stmts
}


Naseem
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Hi,


can we find this for loop in SCJP1.4.


tHANKS
+Pie Number of slices to send: Send
 

Hi,


can we find this for loop in SCJP1.4.



NO. Thats why I wrote it enhanced for-loop. Its added in j2sdk 1.5 and it is covered in SCJP 1.5 exam not in SCJP 1.4

Naseem
+Pie Number of slices to send: Send
That for loop works fine assuming the Dog is-a Animal
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 574 times.
Similar Threads
Tricky Question about Continue Statement
about arrays
Reference Variable Casting?
Reference variable casting
Generics Doubt in for loop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:59:13.