Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop

 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can You Explain,how this loop works



Animal [] a = {new Animal(), new Dog(), new Animal()};
for(Animal animal : a)
{
//stmts
}
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The For-Each Loop
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


can we find this for loop in SCJP1.4.


tHANKS
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ramamoorthy Periasamy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That for loop works fine assuming the Dog is-a Animal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic