aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop" Watch "for loop" New topic
Author

for loop

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
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

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The For-Each Loop
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,


can we find this for loop in SCJP1.4.


tHANKS
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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

Joined: Feb 06, 2006
Posts: 30
That for loop works fine assuming the Dog is-a Animal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop