/** * Calculate the factorial of n (n! = 1 * 2 * 3 * … * n). * * @param n the number to calculate the factorial of. * @return n! - the factorial of n. */ static int fact(int n) { // Base Case: // If n <= 1 then n! = 1. if (n <= 1) { return 1; } // Recursive Case: // If n > 1 then n! = n * (n-1)! else { return n * fact(n-1); } }
如果你对递归技术已经比较了解,那么接下来介绍一下在 NIO.2 中使用到的递归技术。
很多程序都需要访问目录树中的所有文件,这是使用递归操作的好时机。在进行删除、拷贝和移动目录树的时候经常会使用到递归操作。基于此,NIO.2 将递归遍历文件树的过程封装到了一个接口中,这个接口就是 java.nio.file.FileVisitor。
文章来源: http://www.aptusource.org/2014/04/nio-2-recursive-operations/