I'm trying to rewrite this for loop into a for each loop.
int k = 0;
while(k < farmArray.length) {
System.out.println(farmArray[k].getOwner());
k++;
}
This is what i've tried
int k = 0;
for(int Farm:farmArray)
{
System.out.println(farmArray[k].getOwner());
k += Farm;
}
can anyone point me in the right direction? thanks.
for (Farm farm : farmArray) {
System.out.println(farm.getOwner());
}
I think you were over-thinking it... :)
Remove your index completely.
for(Farm farm : farmArray) {
System.out.println(farm.getOwner());
}
Or you can keep track of you index
int k = 0;
for(Farm farm : farmArray) {
System.out.println("Farm #" + k + " is owned by: " + farm.getOwner());
k++;
}
But you should use a for loop for this
for(int k = 0; k < farmArray.length; k++) {
System.out.println("Farm #" + k + " is owned by: " + farmArray[k].getOwner());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.