13
递归
- 自身调用自身
条件 : 必须有出口 , 若没有出口则是一个死递归 , 死递归的最终结果就是栈溢出
斐波那契数列 (兔子数列)
概述 :
斐波那契数列(Fibonacci Sequence) , 又称黄金分割数列 , 又称兔子数列 .
指如下数列 : 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 …
后一个数为前两个数之和
//斐波那契数列
public class FibonacciSequence {
public static void main(String[] args) {
int rabbit = getRabbit(12);
System.out.println(rabbit);
}
private static int getRabbit(int i) {
if (i == 1 | i == 2) {
return 1;
} else {
return getRabbit(i-1)+getRabbit(i-2);
}
}
}
数组高级
排序
选择排序(SelectSort)
概述 :
从第一个位置元素到倒数第二个位置元素依次和后面的每个元素进行比较 , 依次得到每个位置上的最值
//选择排序
public class SelectSort {
public static void main(String[] args) {
int[] arr = {4,2,7,8,5,9,6,3,0};
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+ 1; j < arr.length; j++) {
if (arr[i]>arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
冒泡排序(BubbleSort)
概述 :
重复地走过要排序的序列 , 一次比较两个元素 , 如果两个元素的顺序错误就将其交换 . 重复地进行直到没有再需要交换的元素 , 则该序列排序完成
//冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int[] arr = { 4, 2, 7, 8, 5, 9, 6, 3, 0 };
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
快速排序(QuickSort)
图解 :
//快速排序
public class QuickSort {
public static void main(String[] args) {
int[] arr = { 4, 2, 7, 8, 5, 9, 6, 3, 0};
// int[] arr = { 0 };
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void quickSort(int[] arr, int i, int j) {
if (i > j) {
return;
}
int low = i;
int high = j;
int base = arr[low];
while (i < j) {
// 右边值先动
while (arr[j] >= base && i < j) {
j--;
}
// 左边再动
while (arr[i] <= base && i < j) {
i++;
}
if (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// i==j arr[i]和arr[j]是指同一个数
arr[low] = arr[i];
arr[i] = base;
quickSort(arr, low, i - 1);
quickSort(arr, i + 1, high);
}
}
查找
二分查找(BinarySearch)
概述 :
也叫折半查找 , 前提是数组必须是有序的 , 每次找到数组的中间元素作比较
图解 :
//二分查找
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {45,24,56,1,5,8,32 };
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int index = binarySearch(arr,32);
System.out.println(index);
}
private static int binarySearch(int[] arr,int i) {
int min = 0;
int max = arr.length - 1;
int mid = (min + max)/2;
while(max >= min) {
if (i > arr[mid] ) {
min = mid + 1;
}else if (i < arr[mid]) {
max = mid - 1;
}else {
return mid;
}
mid = (min + max)/2;
}
return -1;
}
}
Arrays工具类
Java封装的数组工具类
- 此类包含用来操作数组(比如排序和查找)的各种方法 .
- 除非特别注明 , 否则如果指定数组引用为null , 则此类中的方法都会抛出 NullPointerException .
使用时建议查看API
包装类
概述 :
- java是一个面向对象的编程语言 , 但是java中的八中基本数据类型却不是面向对象的 , 为了方便使用 , java对其进行了封装 , 此八种基本数据类型对应的类型统称为包装类 , 均位于lang包下 .
String str = "100"; //将str转为int类型
int s = (int)str; //无法进行强制转换,因此需要引用Integer类型
byte------------>Byte
short------------>Short
int------------>Integer
long------------>Long
double------------>Double
float------------>Float
char------------>Character
boolean------------>Boolean
和String相互转换
装箱 : 把基本类型转成包装类
拆箱 : 把包装类转成基本类型
//public Integer(int value) 把int类型转成Integer类
//public Integer(String s) 把String类型转成Integer类
public class IntegerDemo1 {
public static void main(String[] args) {
Integer in = new Integer(13);
Integer i = 15;
int a = 20;
i = i + a;
System.out.println(i); //Integer类型
System.out.println(in);
Integer in2 = new Integer("12345");
System.out.println(in2);
}
}
Integer类
/**常用的基本禁止类型*/
public static String toBinaryString(int i) //转成2进制的类型
public static String toOctalString(int i) //转成8进制的类型
public static String toHexString(int i) //转成16进制的类型
/**十进制转其他进制*/
public static Sring toString(int i) //int类型转为数字字符串类型
public static String toString(int i,int radix) //radix: 转成几进制
/**其他进制转十进制*/
public static int parseInt(String s) //数字字符串类型转为int类型
public static int parseInt(String s,int radix) //s:其他进制的字符串
public class IntegerDemo2 {
public static void main(String[] args) {
String s1 = Integer.toBinaryString(100);
System.out.println(s1);
String s2 = Integer.toOctalString(100);
System.out.println(s2);
String s3 = Integer.toHexString(100);
System.out.println(s3);
String s4 = Integer.toString(100, 2);
System.out.println(s4);
int i = Integer.parseInt("100", 2);
System.out.println(i);
}
}
Charcater类
public static boolean isUpperCase(char ch) //判断字符是否为大写
public static boolean isLowerCase(char ch) //判断字符是否为小写
public static boolean isDigit(char ch) //判断字符是否为数字
public static char toUpperCase(char ch) //将字符转为大写
public static char toLowerCase(char ch) //将字符转为小写
public class CharacterDemo1 {
public static void main(String[] args) {
String s = "JKLhHKKjhkHKJHkgk*(n^*&6788)()9090JJLLJhbjkkljl";
int big = 0;
int small = 0;
int digit = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isUpperCase(ch)) {
big++;
}else if (Character.isLowerCase(ch)) {
small++;
}else if (Character.isDigit(ch)) {
digit++;
}
}
System.out.println("大写字母"+big);
System.out.println("小写字母"+small);
System.out.println("数字"+digit);
}
}
Math类
- 数学工具类
public static int abs(int a) //求绝对值
public static double ceil(double a) //向上取整
public static double floor(double a) //向下取整
public static double pow(double a,double b) //a的b次方
public static double random() //求随机数: [0-1)
public static int round(float a) //四舍五入取整
public static double sqrt(double a) //求算数平方根
附:
round方法表达式 : (int)Math.floor(a + 0.5f)
注意a为负数时
public class MathDemo1 {
public static void main(String[] args) {
int a = -12;
System.out.println(Math.abs(a));
double d = -12.3;
System.out.println(Math.ceil(d));
System.out.println(Math.floor(d));
System.out.println(Math.pow(2, 3));
System.out.println(Math.random());
System.out.println((int)(Math.random()*100+1));
System.out.println(Math.round(d));
System.out.println(Math.sqrt(9));
}
}
System类
public static void gc() //运行垃圾回收器
public static void exit(int status) //终止当前正在运行的Java虚拟机
public static long currentTimeMillis() // 获取当前时间的毫秒值 计算机的世界协调时1970年1月1日0:0:0
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
//
public class SystemDemo1 {
public static void main(String[] args) {
long l = System.currentTimeMillis();
System.out.println(l);
System.out.println(l / 1000 / 60 / 60 / 24 / 365);
System.out.println(System.nanoTime()); //返回最准确的可用系统计时器的当前值,以毫微秒为单位。
}
}
Date类
Date类需要导包: java.util.Date
Date类概述 : 类Date表示特定的瞬间,精确到毫秒。
public Date() //表示当前时间的一个对象
public Date(long date) //得到指定long值对应时间的对象
public long getTime() //把日期对象转成毫秒值
public void setTime(long time) //将日期设置为毫秒值对应的时间
public class DateDemo1 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");
Date date = new Date();
String s = sdf.format(date);
System.out.println(s);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s2 = "1999年1月30日 1:30:00";
Date parse = sdf2.parse(s2);
System.out.println(parse);
long t1 = parse.getTime();
long t2 = System.currentTimeMillis();
long t3 = t2 - t1;
System.out.println(t3 / 1000 / 60 / 60 / 24);
}
}