import java.io.File;
import java.io.IOException;
public class file {
public static void main(String[] args) throws IOException {
File f=new File("E:\\Blog");
printFile(f,0);//打印方法
}
private static void printFile(File file, int level) {
// TODO Auto-generated method stub
for(int i=0;i<level;i++){
System.out.print("-");
}
System.out.println(file.getName());
if(file.isDirectory()){
File[] files =file.listFiles();
for(File temp:files){
printFile(temp,level+1);//递归
}
}
}
}
运行效果:
public class BinarySearch {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println(searchRecursive(array, 0, array.length - 1, 20));
}
/**
* 执行递归二分查找,返回第一次出现该值的位置 *
* @param array 已排序的数组
* @param start 开始位置
* @param end 结束位置
* @param findValue 需要找的值
* @return 值在数组中的位置,从0开始。找不到返回-1
*/
public static int searchRecursive(int[] array, int start, int end,
int findValue) {
// 如果数组为空,直接返回-1,即查找失败
if (array == null) {
return -1;
}
if (start <= end) {
// 中间位置
int middle = (start + end) / 1;
// 中值
int middleValue = array[middle];
if (findValue == middleValue) {
// 等于中值直接返回
return middle;
} else if (findValue < middleValue) {
// 小于中值时在中值前面找
return searchRecursive(array, start, middle - 1, findValue);
} else {
// 大于中值在中值后面找
return searchRecursive(array, middle + 1, end, findValue);
}
} else {
// 返回-1,即查找失败
return -1;
}
}
}
运行结果:-1