【算法篇】递归
一、遞歸的概念
程序調用自身的編程技巧稱為遞歸。
遞歸的核心思想就是將一個大規模復雜的問題層層轉化為一個與原問題相似的規模較小的問題來求解。
二、遞歸的優點
使用遞歸的好處是只需要少量的代碼就可以描述出求解問題過程中多次重復的計算,大大減少了程序的代碼量。
三、遞歸的缺點
在時間和空間的復雜度上往往不是最優的。
四、實現遞歸的條件
一般來說,遞歸要有邊界、遞歸前進段和遞歸返回段。當不滿足邊界條件時,遞歸前進,當滿足邊界條件時,遞歸返回。
五、示例
使用遞歸將一個文件夾中的所有文件拷貝到另外一個文件夾
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;/** 使用遞歸將一個文件夾中的所有文件拷貝到另外一個文件夾*/ public class Recursion {public final String srcPath;public final String targetPath;public Recursion(String srcPath, String targetPath) {this.srcPath = srcPath;this.targetPath = targetPath;}public String getSrcPath() {return srcPath;}public String getTargetPath() {return targetPath;}public static void main(String[] args) throws IOException {Recursion recursion = new Recursion("F:\\固態盤之前\\MyWorkSpace\\MyEclipse 9\\day13","C:\\Users\\cc\\Desktop\\day13");recursion.iteratorFile(recursion.getSrcPath());}/** 遞歸方法*/public void iteratorFile(String srcPath) {if (srcPath != null && !srcPath.equals("")) {File file = new File(srcPath);if (file.isDirectory()) {File[] files = file.listFiles();//遍歷當前目錄下的所有文件和目錄for (int i = 0; i < files.length; i++) {if (files[i].isDirectory()) {iteratorFile(files[i].getAbsolutePath());} else {//開啟一個線程拷貝文件new ThreadCopy(files[i].getAbsolutePath(),files[i].getAbsolutePath().replace(this.getSrcPath(), this.getTargetPath())).start();}}} else {//開啟一個線程拷貝文件new ThreadCopy(file.getAbsolutePath(),file.getAbsolutePath().replace(this.getSrcPath(), this.getTargetPath())).start();}}} }class ThreadCopy extends Thread {private String src;private String tar;public ThreadCopy(String src, String tar) {this.src = src;this.tar = tar;}@Overridepublic void run() {FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream bos = null;try {File file = new File(tar);//如果拷貝文件的目標目錄不存在,則創建目標目錄File directory = new File(file.getParent());if (!directory.exists()) {directory.mkdirs();}fis = new FileInputStream(src);bis = new BufferedInputStream(fis);fos = new FileOutputStream(tar);bos = new BufferedOutputStream(fos);byte[] buffer = new byte[1024];int len = 0;while ((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}bos.flush();System.out.println(Thread.currentThread().getName() + ":拷貝完了!");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//關閉流close(bos, fos);close(bis, fis);}}public void close(OutputStream oStream1, OutputStream oStream2) {if (oStream1 != null) {try {oStream1.close();} catch (IOException e) {e.printStackTrace();}}if (oStream2 != null) {try {oStream2.close();} catch (IOException e) {e.printStackTrace();}}}public void close(InputStream iStream1, InputStream iStream2) {if (iStream1 != null) {try {iStream1.close();} catch (IOException e) {e.printStackTrace();}}if (iStream2 != null) {try {iStream2.close();} catch (IOException e) {e.printStackTrace();}}}}?
?
總結
- 上一篇: 上海大众桑塔纳3000进气系统的组成
- 下一篇: 福特翼搏水箱回水管能接吗?