i watch in a tutorial that i can traverse an array of object in this way:
Animals[] an = new Animals[2];
for (Animals a:an){
.
.
}
But now i want to traverse a 2 dimensional array and when i use this code i have a problem(says:incompatible types
required:appl1.Animals
foundLappl1.Animals[]). when i use this code
Animals[][] an = new Animals[2][2];
for (Animals a:an){
.
.
}
Does anyone knows how can i overcome this problems. Thank you in advance for your help.
You will need to use nested loops, as follows:
Why does this work?
Look at your first example (reposted here for convenience):
This works because
anis an array ofAnimalsobjects. Theforloop iterates through eachAnimalsobject, performing some action on them one-by-one.Now look at my answer posted above (again, reposted here for context):
This works because
anis an array ofAnimals[]objects. The firstforloop iterates through eachAnimals[]. At that point, you have an array of Animals objects, so you can use the same solution as above: a singleforloop to iterate through each of theAnimalsobject and perform some action on them one-by-one.