Problem

Challenge exercise The for-each loop does not use an explicit integer variable to a...

Challenge exercise The for-each loop does not use an explicit integer variable to access successive elements of the list. Thus, if we want to include the index of each file name in the listing, then we would have to declare our own local integer variable (position, say) so that we can write in the body of the loop something like:

System.out.println(position + ": " + filename);

See if you can complete a version of listAllFiles to do this. Hint: You will need a local variable declaration of position in the method, as well as a statement to update its value by one inside the for-each loop.

One of the things this exercise illustrates is that the for-each loop is not really intended to be used with a separate index variable.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT