第1周学习
这周主要学习了Java的知识,日期Calendar,Instant,ZoneDataTime,LocalTIme LocalDate LocalDateTime,计算时间的Duration Period ChronoUnit类
首次接触数据库MySQL,
Calendar
import java.util.Calendar;
import java.util.Date;
public class Test1 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
Date d = new Date();
c.setTime(d);
c.set(Calendar.YEAR,2018);
c.add(Calendar.MONTH,1);
int year = c.get(Calendar.YEAR); //c实际上是个数组,等价c.get(1);
int month = c.get(Calendar.MONTH) + 1; //0 - 11 所以获取月份要加1
int day = c.get(Calendar.DATE);
int week = c.get(Calendar.WEEK_OF_MONTH);
System.out.println(year + "-" + month + "-" + day + "-" + getWeek(week));
}
//查表法
public static String getWeek(int index){
String[] week = {"日","一","二","三","四","五","六"};
return "星期" + week[index - 1];
}
}
时区时间和格式化
ZoneId
import java.time.ZoneId;
import java.util.Set;
public class ZoneId_Test1 {
public static void main(String[] args) {
Set<String> s = ZoneId.getAvailableZoneIds(); //获取所有时区的集合'
System.out.println(s);
ZoneId z1 = ZoneId.systemDefault(); //获取系统当前时区
System.out.println(z1);
ZoneId z2 = ZoneId.of("America/El_Salvador"); //指定时区
System.out.println(z2);
}
}
Instant时间戳
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Instant_Test2 {
public static void main(String[] args) {
Instant i = Instant.now();
System.out.println(i);
Instant i2 = Instant.ofEpochSecond(2L);
System.out.println(i2);
Instant i3 = Instant.ofEpochMilli(30000L);
System.out.println(i3);
ZonedDateTime zd = Instant.now().atZone(ZoneId.of("America/El_Salvador"));
System.out.println(zd);
System.out.println(i.isAfter(i2));
System.out.println(i2.isBefore(i));
System.out.println(i2.minusSeconds(1L)); //时间减1秒
System.out.println(i2.plusSeconds(1L)); //时间减1秒
}
}
ZoneDateTime
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTime_Test {
public static void main(String[] args) {
ZonedDateTime zd = ZonedDateTime.now();
System.out.println(zd);
ZonedDateTime zd2 = ZonedDateTime.of(2003,12,1,10,50,0,0, ZoneId.of("Asia/Shanghai"));
System.out.println(zd2);
System.out.println(zd2.withHour(8)); //修改小时
System.out.println(zd2.minusHours(2)); //修改小时
System.out.println(zd2.plusDays(1)); //修改天
//修改,增加每减少都会新建一个新的对象
}
}
DateTimeFormatter
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatter_Test {
public static void main(String[] args) {
ZonedDateTime zdf = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
System.out.println(dtf.format(zdf));
}
}
LocalTIme LocalDate LocalDateTime
LocalDateTime 转换为 LocalTime 和 LocalDate
import java.time.LocalDate;
import java.time.LocalDateTime;
public class LocalDateTime_Test {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
System.out.println(ldt.getYear());
System.out.println(ldt.getMonthValue());
System.out.println(ldt.getDayOfMonth());
LocalDate ld = ldt.toLocalDate();
System.out.println(ld);
System.out.println(ldt.toLocalTime());
}
}
Duration Period ChronoUnit
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class ChronoUnit_Test {
public static void main(String[] args) {
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthday = LocalDateTime.of(2024,8,20,0,0,0);
System.out.println(ChronoUnit.YEARS.between(birthday,today));
System.out.println(ChronoUnit.MONTHS.between(today,birthday));
System.out.println(ChronoUnit.DAYS.between(today,birthday));
System.out.println(ChronoUnit.SECONDS.between(today,birthday));
}
}
包装类
Integer
public class Integer_Test {
public static void main(String[] args) {
Integer i1 = new Integer(5);
System.out.println(i1);
Integer i2 = Integer.valueOf(10);
System.out.println(i2);
}
}
包装类:用一个对象,把一个基本数据类型包装起来(基本数据类型对应的引用类型)
在JDK5的时候提出了一个机制,自动装箱和自动拆箱
自动装箱:把基本数据类型自动变成其对应的包装类
自动拆箱:把包装类自动变成其对对象的基本数据类型
所以在JDK5以后,int和Integer可以看做同一个东西,因为在内部可以自动转换
Integer的成员方法
System.out.println(Integer.toBinaryString(i2));
System.out.println(Integer.parseInt("45555"));
sc.nextLine() //遇到回车停止
import java.util.Scanner;
public class Integer_Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
//在8种包装类中,除了Character都有其对应的parseXXX转换方法,进行类型转化
double d = Double.parseDouble(line);
System.out.println(d);
}
}
实现Integer.parseInt()函数
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
String regex = "[1-9][0-9]{0,9}";
if(num.matches(regex)){
int number = 0;
for(int i = 0; i < num.length(); i++){
number = number * 10 + (num.charAt(i) - '0');
}
System.out.println(number);
}
else System.out.println("格式错误");
}
}
十进制转换为二进制字符串
public class Test4 {
public static void main(String[] args) {
int number = 123;
System.out.println(toBinaryString(number));
}
public static String toBinaryString(int number){
StringBuilder sb = new StringBuilder();
while(number != 0){
sb.append(number%2);
number /= 2;
}
return sb.reverse().toString();
}
}
计算活了多少天(时间计算)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class Test5 {
public static void main(String[] args) throws ParseException {
//JDK7
String birthday = "2005-08-20";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayTime = sdf.parse(birthday);
long today = System.currentTimeMillis();
System.out.println((today - birthdayTime.getTime())/1000/60/60/24);
//JDK8
LocalDate birthday2 = LocalDate.of(2005,8,20);
LocalDate today2 = LocalDate.now();
System.out.println(ChronoUnit.DAYS.between(birthday2,today2));
}
}
判断是否为闰年
import java.text.ParseException;
import java.time.LocalDate;
public class Test5 {
public static void main(String[] args) throws ParseException {
//JDK8
LocalDate birthday2 = LocalDate.of(2005,8,20);
System.out.println(birthday2.isLeapYear());
}
}
二分查找
public class BinarySearch_Test {
public static void main(String[] args) {
int[] arr = {1,5,8,6,12,56,66,78,88};
System.out.println(binarySearch(arr, 1));
}
public static int binarySearch(int[] arr,int number){
int min = 0, max = arr.length,mid = 0;
while(min <= max){
mid = (min + max)/2;
if(arr[mid] > number) max = mid - 1;
else if(arr[mid] < number) min = mid + 1;
else return mid;
}
return -1;
}
}
二分查找升级版(插值查找)
//插值查找
public static int interpolationSearch(int[] arr ,int number){
int min = 0, max = arr.length - 1,mid = 0;
while(min <= max){
mid = (min + max)/2;
if(arr[mid] > number){
max = mid - (arr[max] - number)/arr[max] - arr[min] * (max - min);
}
else if(arr[mid] < number){
min = mid + (number - arr[min])/(arr[max] - arr[min]) * (max - min);
}
else return mid;
}
return -1;
}
分块查找
class Block{
private int max;
private int start;
private int end;
public Block() {
}
public Block(int max, int start, int end) {
this.max = max;
this.start = start;
this.end = end;
}
/**
* 获取
* @return max
*/
public int getMax() {
return max;
}
/**
* 设置
* @param max
*/
public void setMax(int max) {
this.max = max;
}
/**
* 获取
* @return start
*/
public int getStart() {
return start;
}
/**
* 设置
* @param start
*/
public void setStart(int start) {
this.start = start;
}
/**
* 获取
* @return end
*/
public int getEnd() {
return end;
}
/**
* 设置
* @param end
*/
public void setEnd(int end) {
this.end = end;
}
public String toString() {
return "Block{max = " + max + ", start = " + start + ", end = " + end + "}";
}
}
public class blockSearch_Test {
public static void main(String[] args) {
int[] arr = {16,5,9,12, 21,18,
32,23,37,26,45,34,
50,48,61,52,73,66};
Block b1 = new Block(21,0,5);
Block b2 = new Block(45,6,11);
Block b3 = new Block(73,12,17);
Block[] blockArr = {b1,b2,b3};
int number = 45;
System.out.println(findIndex(arr,blockArr,number));
}
public static int findBlock(Block[] blockArr,int number){
for(int i = 0; i < blockArr.length; i++){
if(blockArr[i].getMax() >= number)return i;
}
return -1;
}
public static int findIndex(int[] arr,Block[] blockArr,int number){
int site = findBlock(blockArr,number);
for(int i = blockArr[site].getStart(); i <=blockArr[site].getEnd(); i++){
if(number == arr[i])return i;
}
return -1;
}
}
插值排序
public class insertSort_Test {
public static void main(String[] args) {
int[] arr = {1,8,6,4,2,9,7,0,5,3,4};
int startIndex = -1;
for (int i = 0; i < arr.length; i++) {
if(arr[i] > arr[i+1]){
startIndex = i + 1;
break;
}
}
//System.out.println(startIndex);
for(int i = startIndex; i < arr.length; i++){
int j = i;
while(j > 0 && arr[j] < arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
快速排序
public static void quickSort(int[] arr,int i,int j) {
int start = i, end = j;
if(start > end) return;
int baseNumber = arr[i];
while (start != end) {
while (true) {
if (start == end || arr[end] < baseNumber) {
break;
}
end--;
}
while (true) {
if (start == end || arr[start] > baseNumber) {
break;
}
start++;
}
int t = arr[end];
arr[end] = arr[start];
arr[start] = t;
}
int t = arr[start];
arr[start] = arr[i];
arr[i] = t;
quickSort(arr, i, start - 1);
quickSort(arr, start + 1, j);
}
}
Arrays
import java.util.Arrays;
import java.util.Comparator;
public class Arrays_Test {
public static void main(String[] args) {
int[] arr = {5,4,1,2,3};
System.out.println(Arrays.binarySearch(arr,1));
//Arrays.fill(arr,5);
//F(arr);
System.out.println(Arrays.toString(arr));
int[] arr2 = Arrays.copyOf(arr,6);
F(arr2);
int[] arr3 = Arrays.copyOfRange(arr,2,4); //包头不包尾
F(arr3);
Arrays.sort(arr);
F(arr);
Integer[] arr4 = {5,4,1,2,3};
Arrays.sort(arr4, new Comparator<Integer>() { //匿名内部类
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < arr4.length; i++) {
System.out.print(arr4[i] + " ");
}
}
public static void F(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Lambda表达式
函数式编程
函数式编程是一种思想特点,面向对象:先找对象,让对象做事情
- Lambda表达式可以用来简化匿名类的书写
- Lambda表达式只能简化函数式接口的匿名内部类的写法
- 函数式接口: 有且仅有一个抽象方法的接口叫做函数式接口,接口上方可以加@FunctionalInterface注释
public class Lambda_Test{
public static void main(String[] args) {
//1.利用匿名内部类实现
method(new Swim(){
@Override
public void swimming() {
System.out.println("游泳--");
}
});
//2.使用lambda表达式
method(()->{
System.out.println("游泳??");
});
}
public static void method(Swim s){
s.swimming();
}
}
interface Swim{
public abstract void swimming();
}
import java.util.Arrays;
public class Lambda_Test{
public static void main(String[] args) {
String[] arr = {"a","aaaa","aaa","aa"};
//lambda表达式
Arrays.sort(arr,(o1,o2)->{return o1.length() - o2.length();});
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
集合体系结构
List系列结合:添加的元素是有序,可重复,有索引
Set系列集合:添加的元素是无序,不重复,无索引
Collection
public boolean add (E e) 添加
public void clear() 清空
public boolean remove (E e) 删除
public boolean contains(Object obj) 判断是否包含
public boolean isEmpty() 判断是否为空
public int size() 集合长度
contains方法,底层是依赖equals方法进行判断是否存在的
所以,如果集合中存储的是自定义对象,也想通过contains方法来判断是否包含,那么在javabean类中,一定要重写equals方法
Collection的遍历方式
迭代器遍历
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Collection_Test {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<String>();
coll.add("aaa");
coll.add("bbb");
coll.add("ccc");
coll.add("ddd");
//迭代器遍历容器
Iterator<String> it = coll.iterator();
while(it.hasNext()) System.out.println(it.next());
}
}
迭代器遍历完毕,指针不会复位.
迭代器的细节注意点:
- 报错NoSuchElementException
- 迭代器遍历完毕,指针不会复位
- 循环中只能用一次next方法
- 迭代器遍历时,不能用集合的方法进行增加和删除,如果要删除,可以用迭代器提供的remove方法删除
增强for遍历
- 增强for的底层就是迭代器,为了简化迭代器的代码书写的
- 它是JDK5之后出现的,其内部原理就是一个Iterator迭代器
- 所有的单列集合和数组才能用增强for进行遍历
import java.util.ArrayList;
import java.util.Collection;
public class a02for_Test {
public static void main(String[] args) {
Collection<Integer> coll = new ArrayList<>();
for (int i = 0; i < 5; i++) {
coll.add(i);
}
//增强for循环
for(int i : coll){
System.out.println(i);
}
}
}
lambda表达式遍历
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;
public class a03Lambda_Test {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<>();
coll.add("aaa");
coll.add("bbb");
coll.add("ccc");
//匿名内部类
coll.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
//lambda表达式遍历
coll.forEach( s -> System.out.println(s));
}
}
MySQL知识点
数据库相关概念
数据库:存储数据的仓库,数据是有组织的进行存储 DataBase(DB)
数据库管理系统:操控和管理数据库的大型软件 DataBase Management System(DBMS)
SQL: 操作关系型数据库的编程语言,定义了一套操作关系型数据库统一标准 Structured Query Language(SQL)
主流的关系型数据库管理系统
Oracle,MySQL,SQL Server
SQL是统一标准
启动mysql两种方式
- win + r 输入services.msc
- 管理员模式 在命令行输入 net start mysql80 / ner stop mysql80
连接mysql的两种方式
方式一:打开MySQL 8.0 Command Line Cliend
方式二:命令方式mysql -u root -p
SQL
SQL通用语法
SQL分类
- DDL (Data Definition Language)
- DML (Data Manipulation Language)
- DQL (Data Query Language)
- DCL (Data Control Language)
DDL数据库操作
查询 : show databases;
查询当前数据库: select database();
创建: creat databases [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];
删除:drop databases[if exists]数据库名;
使用:use 数据库名;
DDL-表操作-查询
- 查询当前数据库所有表
show tables;
- 查询表结构
desc 表名;
- 查询指定表的建表语句
show create table 表名;
DDL-表操作-创建
mysql> create table tb_user(
-> id int comment '编号',
-> name varchar(50) comment '姓名',
-> age int comment '年龄',
-> gendar varchar(1) comment '性别'
-> ) comment '用户表';
DDL-表操作-数据类型
DDL-表结构-修改
添加字段 alter table 表名 ADD字段名 类型(长度)[COMMENT 注释]][约束]
;
修改数据类型alter table 表名 modify 字段名 新数据类型(长度);
修改字段名和字段类型alter table 表名 change 旧的字段名 新字段名 类型(长度) [comment 注释][约束];
删除字段alter table 表名 drop 字段名;
修改表名alter table 表名 rename to 新表名;
删除表drop table [if exists]表名;
删除指定表,并重新创建该表truncate table 表名;