2021-1-27
1.递归
案例
package com.wc.show;
/**
* @author wc
* @Date: 2021/01/27/16:29
*/
public class Factorial {
public static void main(String[] args) {
int result=getJc(5);
System.out.println(result);
}
private static int getJc(int i) {
//出口,当参数为多少时,递归结束
if (i==1){
return 1;
}else{
return i*getJc(i-1);
}
}
}
2.快速排序
代码实现
package com.wc.show;
/**
* @author wc
* @Date: 2021/01/27/16:50
*/
public class Quick {
public static void main(String[] args) {
int[] arr = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
quiteSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
private static void quiteSort(int[] arr, int left, int right) {
if (left>right){
return;
}
int left0 = left;
int right0 = right;
//循环条件,当左等于右时停止循环
//基准数默认为第一个
int baseNumber =arr[left0];
while (left != right) {
//1.从右开始找比基准数小的
while (arr[right] >= baseNumber && right > left) {
right--;
}
//2.从左开始找比基准数大的
while (arr[left] <= baseNumber && right > left) {
left++;
}
//3.左右数值交换
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//基准数归位
int temp=arr[left];
arr[left]=arr[left0];
arr[left0]=temp;
quiteSort(arr,left0,left-1);
quiteSort(arr,left+1,right0);
}
}
3.Arrays类和常用方法
package com.wc.show;
import java.util.Arrays;
/**
* @author wc
* @Date: 2021/01/28/9:30
*/
public class MyArrays {
public static void main(String[] args) {
//toString 将数组按照字符串形式返回
int [] arr={3,1,2,6,7};
System.out.println(Arrays.toString(arr));
//sort 按照数字顺序排列指定数组
int [] arr1={3,1,2,6,7};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
}
}
4.日期类(Date有参无参构造、获取当前时间)
package com.wc.show;
import java.util.Date;
/**
* @author wc
* @Date: 2021/01/28/9:54
*/
public class DateDemo {
public static void main(String[] args) {
//空参构造 获取当前系统时间
Date date=new Date();
System.out.println(date);
//有参构造 从时间原点开始(1970.1.1),过了多少时间(指定毫秒值)
Date date1=new Date(3600L*1000);
System.out.println(date1);
//获取当前时间毫秒值
Date date2=new Date();
long time = date2.getTime();
System.out.println(time);
long l = System.currentTimeMillis();
System.out.println(l);
//设置时间
Date date3=new Date();
date3.setTime(0L);
System.out.println(date3);
}
}
5.SimpleDateFormat类(构造方法,格式化,解析日期)
package com.wc.show;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author wc
* @Date: 2021/01/28/10:01
*/
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//SimpleDateFormat 构造方法
//空参,默认格式
//SimpleDateFormat sdf = new SimpleDateFormat();
//有参,自定义格式
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//格式转换 date->string format
String s = sdf.format(date);
System.out.println(s);
//格式转换 string->date parse
String s1 = "2021年01月28日 10:16:35";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date parse = simpleDateFormat.parse(s1);
System.out.println(parse);
}
}
案例
package com.wc.show;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author wc
* @Date: 2021/01/28/10:41
*/
public class SimpleDateFormatDemo1 {
public static void main(String[] args) throws ParseException {
String startTime="2020年11月11号 0:0:0";
String endTime="2020年11月11号 0:10:0";
String jia="2020年11月11号 0:03:47";
String pi="2020年11月11号 0:10:11";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd号 HH:mm:ss");
//String->date date 调用getTime方法,获取毫秒值
long time1 = sdf.parse(startTime).getTime();
long time2 = sdf.parse(endTime).getTime();
long time3 = sdf.parse(jia).getTime();
long time4 = sdf.parse(pi).getTime();
if (time3>=time1&&time3<=time2){
System.out.println("成功");
}else {
System.out.println("失败");
}
if (time4>=time1&&time4<=time2){
System.out.println("成功");
}else {
System.out.println("失败");
}
}
}
6.JDk8时间工具类(LocalDateTime构造方法,常用方法,格式化和解析)
LocalDateTime创建方法
package com.wc.show;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* @author wc
* @Date: 2021/01/28/11:58
*/
public class Local {
public static void main(String[] args) {
//LocalDateTime 构造方法 获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
System.out.println(localDateTime);
}
}
LocalDateTime获取方法
LocalDateTime转换方法
LocalDateTime格式化和转换方法
package com.wc.show;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* @author wc
* @Date: 2021/01/28/11:58
*/
public class Local {
public static void main(String[] args) {
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
System.out.println(localDateTime1);
//LocalDateTime格式化成字符串
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s = localDateTime1.format(pattern);
System.out.println(s);
//把一个日期字符串解析成LocalDateTime格式
String date="2020年11月11日 11:11:11";
DateTimeFormatter pattern1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(date,pattern1);
System.out.println(parse);
}
}
LocalDateTime增加或者减少时间
LocalDateTime修改方法
7.JDk8时间小结
8.异常
9.throws和throw
10.异常处理方式
package com.wc.show;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/01/28/17:16
*/
public class ExceptionUse {
public static void main(String[] args) {
//1.如果try中没出现问题,怎么执行?
//会把try中所有代码执行,不会执行catch里面代码
//2.如果try中出现问题,try下面代码还会执行吗?
//直接跳转到对应的catch中,try下面代码不执行
//当catch里面的语句全部执行完毕,表示整个体系执行完毕,继续执行下面的代码
//3.如果try中出现问题的问题没有被捕获,怎么运行?
//那么try...catch相当于没有,也就是没处理
//默认交给虚拟机处理
//4.如果同事出现多个异常,怎么执行?
//出现多个异常,就多些几个catch
//注意点:如果异常之间存在父子关系,父类要写在下面
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
String line = sc.nextLine();
int age = Integer.parseInt(line);
System.out.println(age);
System.out.println(2/0);
} catch (NumberFormatException e) {
System.out.println("格式化异常错误");
}catch (ArithmeticException e){
System.out.println("数学异常");
}
System.out.println("测试");
}
}
11.Throwable成员方法
小结
案例
package com.wc.scanner;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/01/28/19:49
*/
public class StudentScanner {
public static void main(String[] args) {
Student student = new Student();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名");
String name = scanner.nextLine();
student.setName(name);
while (true) {
System.out.println("请输入年龄");
String ageArr = scanner.nextLine();
try {
int age = Integer.parseInt(ageArr);
student.setAge(age);
break;
} catch (NumberFormatException e) {
System.out.println("请输入一个整数");
continue;
} catch (RuntimeException e) {
System.out.println("请输入范围内的数");
continue;
}
}
System.out.println(student);
}
}