Question: Listing 20.7, DirectorySize.java, gives a recursive method for finding a directory size. Rewrite this method without using recursion. Your program should use a queue to

Listing 20.7, DirectorySize.java, gives a recursive method for finding a directory size. Rewrite this method without using recursion. Your program should use a queue to store the subdirectories under a directory. The algorithm can be described as follows:

long getSize (File directory) { long size = 0; add directory to


the queue; while (queue is not empty) { Remove an item from

long getSize (File directory) { long size = 0; add directory to the queue; while (queue is not empty) { Remove an item from the queue into t; if (t is a file) size += t.1ength(); else add all the files and subdirectories under t into the queue; return size;

Step by Step Solution

3.41 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program Plan Create class DirectorySize It contains two method which are mainString args and getSize... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!