javaFile类的使用

在这里插入图片描述

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值