文章目录
- Arrays(数组)
- Collections(集合)
- System(系统)
- Date 时间类
- Executors 线程池
- TimeUnit 时间段
- Math 数学
- String(字符串)
- length(获取字符串长度)
- charAt(获取单个字符)
- getBytes(转为byte数组)
- toLowerCase(转为小写字母)
- toUpperCase(转为大写字母)
- concat(字符串添加)不常用
- contains(判断字符串是否包含该字符)
- startsWith(判断字符串是否以指定字符开头)
- endsWith(判断字符串是否以指定字符结尾)
- indexOf(判断字符在字符串中的位置)
- lastindexOf(判断字符在字符串中的最后出现的位置)
- split(根据分割规则,返回字符串数组)
- substring(切割字符串)
- trim(去除两边的空格)
- equalsIgnoreCase(忽视大小写)
- isEmpty(字符串是否为空)
Arrays(数组)
toString() 把数组转换成字符串
int[] a = {1, 6, 5, 2, 1};
// 把数组 a 转换成字符串 Arrays.toString(a)
System.out.println(Arrays.toString(a));
binarySearch( ) 二分查找
用法:binarySearch(数组,查找的数值 )
细节:
1. 数组中的元素必须是升序的
2. 如果查到的元素是存在的则返回索引,如果不存在则返回 插入点 - 1
解析:-1 的原因
如果查找数字0 0此时是不存在的,那么返回的值就是 -的插入点 就是-0,所以为了避免这种情况久-1
int[] a = {1, 2, 3, 4, 5}; // 查找2 ,在第1位
System.out.println(Arrays.binarySearch(a, 2));
copyOf( ) 拷贝数组(不可以指定范围)
int[] a = {1, 2, 3, 4, 5};
int[] ints = Arrays.copyOf(a, 5);
用法:copyOf(数组,新数组的长度 )
细节:
1. 如果新的数组长度是小于老的数组,那么后面的会省略
2. 如果新的数组长度是大于老的数组,那么会默认值填充
3. 如果新的数组长度是等于老的数组,完全拷贝
copyOfRange( ) 拷贝数组(指定范围)
int[] a = {1, 2, 3, 4, 5};
int[] ints = Arrays.copyOfRange(a, 0, 4);
用法:copyOfRange(数组,开始索引,结束索引 )
细节:
1. 如果结束索引是大于老的数组,那么会默认值填充
2. 拷贝的数值不包含结束索引
fill( ) 填充数组(只可以全部填充)
int[] a = {1, 2, 3, 4, 5};
Arrays.fill(a,25);
用法:fill(数组,填充值 )
sort( ) 按照指定规则排序(默认升序)
用法:sort(数组,排序规则)
排序规则是一个接口,需要传递这个接口的实现类对象作为排序规则
只使用一次,所以使用匿名内部类就可以了
底层原理:
1. 利用插入排序 + 二分查找进行排序
2. 默认把0索引的数据当作有序的序列,把1当作无序的索引
3. Comparator接口类
4. 假设当前遍历的是A元素,利用二分查找找到确认的插入点
5. 拿着A元素和插入点的元素比较,比较规则Comparator
6. 如果返回的是正数或者是0,就继续跟后面的数进行比较
6. 如果返回的是负数,就继续跟前面的数进行比较
直到确定A的最终位置
// 定义包装类
Integer[] arr = {16, 5, 9, 12, 21, 18, 32, 23, 37, 26, 45, 34, 50, 48, 61, 52, 73, 66};
// 修改成 降序排列
Arrays.sort(arr, new Comparator<Integer>() {
// 修改比较规则
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
细节:
1. 排序规则省略就是升序排序
2. 只能给引用数据类型的数组进行排序,如果是基本类型需要变成其对应的包装类
3. 默认升序 o2 - o1降序
Collections(集合)
addAll(添加多个参数)
ArrayList<String> list = new ArrayList<>();
// 参数1 是添加的集合,参数二,是添加的多个元素
Collections.addAll(list,"asad","1223","51dfs");
for (String s : list) {
System.out.println(s);
}
shuffle(打乱集合元素)
只支持 list 集合
ArrayList<String> list = new ArrayList<>();
// 参数1 是添加的集合,参数二,是添加的多个元素
Collections.addAll(list,"asad","1223","51dfs");
// 打乱顺序
Collections.shuffle(list);
for (String s : list) {
System.out.println(s);
}
sort(按照规定排序)
sort | |
---|---|
参数一: | list集合 |
参数二: | 排序规则Comparator |
public static void main(String[] args) throws InterruptedException {
ArrayList<String> list = new ArrayList<>();
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// 这里写排序规则
return 0;
}
});
}
binarySearch(二分查找)
binarySearch | |
---|---|
参数一: | list集合 |
参数二: | 查找的元素 |
前提:必须有序 | 返回索引 |
ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
int key = Collections.binarySearch(list, 3);
System.out.println(key);
因为ArrayList,底层是数组,所以元素必须一个一个添加,直接拷贝,需要先开辟空间,使用addAll先把空元素个数放进去,然后拷贝
ArrayList<String> arrayList1 = new ArrayList<>();
//验证初始化集合元素的个数
System.out.println(arrayList1.size());//输出的结果是0
arrayList1.add("哼哼");
arrayList1.add("小哈");
arrayList1.add("小黑");
// 声明一个目的集合,不规定集合中元素的个数
ArrayList<String> arrayList3 = new ArrayList<>();
//将String类型的空值添加到目的集合中
Collections.addAll(arrayList3, new String[arrayList1.size()]);
//复制集合
Collections.copy(arrayList3, arrayList1);
System.out.println("复制后的集合的元素是" + arrayList3);
//输出结果是 复制后的集合的元素是[哼哼, 小哈, 小黑]
fill(填充元素)
fill | |
---|---|
参数一: | list集合 |
参数二: | 需要填充的元素 |
前提:集合必须有长度 |
ArrayList<Integer> int1 = new ArrayList<>();
int1.add(2);
int1.add(2);
int1.add(2);
Collections.fill(int1,4);
System.out.println(int1);
max / min(最大值 / 最小值)
重点:根据的自然排序比较的
ArrayList<Integer> int1 = new ArrayList<>();
int1.add(2);
int1.add(66);
int1.add(0);
Integer max = Collections.max(int1);
System.out.println(max);
Integer min = Collections.min(int1);
System.out.println(min);
swap(交换元素的位置)
swap | |
---|---|
参数一: | list集合 |
参数二: | 交换的索引1 |
参数三: | 交换的索引2 |
ArrayList<Integer> int1 = new ArrayList<>();
int1.add(2);
int1.add(66);
int1.add(0);
Collections.swap(int1,0,2);
System.out.println(int1);
System(系统)
arraycopy 浅复制(会覆盖原位置的数值)
用法:
System.arraycopy(源数组,源数组起始索引,目标数组,目标数组起始索引,拷贝个数);
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int[] arr1 = {6,7,8,9,10};
// 将源数组的2,3,4拷贝到目标数组中,并覆盖原位置的数值
System.arraycopy(arr,1,arr1,1,3);
// 打印
System.out.println(Arrays.toString(arr1));
}
浅赋值详解:如果是引用数据类型,传递的是地址,而不是数值
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {9, 9, 9}, {1, 2, 3}};
int[][] arr1 = {{6, 7, 8},{6, 7, 8},{6, 7, 8}};
// arr 的 1维数组 999 拷贝给 arr1
System.arraycopy(arr, 1, arr1, 1, 1);
// 修改arr的数组 将第1个9修改为6
arr[1][0] = 6;
// 打印arr1 会发现数值也随着更改勒
System.out.println(Arrays.toString(arr1[1]));
}
exit(结束程序)
传0表示程序正常结束,传1表示异常结束
System.exit(0);
currentTimeMillis(获取时间戳)
long l = System.currentTimeMillis();
System.out.println(l);
Date 时间类
技巧:1000毫秒 = 1秒
一年:time = time + 1000L * 60 * 60 * 24 * 365;
创建当前时间对象
Date d = new Date();
System.out.println(d);
细节:构造方法里面可以传递一个参数,表示毫秒,从1970年1月1日(加上输入的毫秒)
getTime(获取当前时间毫秒)
Date d = new Date();
long time = d.getTime();
System.out.println(time);
细节:返回一个long类型的毫秒值
setTime(设置当前时间毫秒)
Date d = new Date();
d.setTime(1664327303769L);
System.out.println(d);
细节:输入一个long类型的毫秒值,设置为当前时间
SimpleDateFormat(格式化时间)
创建时间格式对象
构造参数:时间的格式
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
格式 | |
---|---|
y | 年 |
M | 月 |
d | 日 |
H | 时 |
m | 分 |
s | 秒 |
format(转换时间)
Date d = new Date();
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
// 传递一个时间对象,转换成字符串返回
String f = s.format(d);
System.out.println(f);
传递一个时间对象,转换成字符串返回
parse(字符串转换成时间对象)
public static void main(String[] args) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
// 先给定一个字符串
String a = "2002年1月26日";
// 把a给parse 转换成时间对象
Date kk = s.parse(a);
System.out.println(kk);
}
传递一个字符串参数,返回一个时间对象,格式必须和SimpleDateFormat 一样,否则报错
Calendar 日历类
代表当前时间的日历对象 , 单独修改时间中的年月日
创建日历对象
Calendar i = Calendar.getInstance();
是抽象类,所以调用方法获取
底层原理:
会根据系统的不同时区获取不同的日历对象表示当前时间
把时间中的纪元 年 月 日 时 分 秒 星期放到数组中
setTime(设置时间)
String mon = "2002年1月26日";
// 设置格式
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
// 把字符串转换为时间
Date parse = s.parse(mon);
// 创建日历类
Calendar i = Calendar.getInstance();
// 把刚刚解析出来的时间,设置给日历类
i.setTime(parse);
System.out.println(parse);
设置时间的参数是date时间对象
getTime(获取当前时间对象)
Calendar i = Calendar.getInstance();
Date time = i.getTime();
System.out.println(time);
get(获取时间常量)
时间常量 | |
---|---|
YEAR | 年 |
MONTH | 月 |
DATE | 日 |
DAY_OF_MONTH | 星期 |
细节 | 月份0~11 0 等同于1月 以此类推 |
星期1 ~ 7 1 等同于 星期日 以此类推 |
Calendar i = Calendar.getInstance();
int year = i.get(Calendar.YEAR);
System.out.println(year);
时间常量,需要类名 . 常量,用get获取,返回int
set(指定常量,数值)
Calendar i = Calendar.getInstance();
// 设置年份
i.set(Calendar.YEAR, 2002);
// 获取年份
System.out.println(i.get(Calendar.YEAR));
给指定时间,设置数值
add(指定常量,在基础上增加数值)
Calendar i = Calendar.getInstance();
i.add(Calendar.YEAR,2);
System.out.println(i.get(Calendar.YEAR));
在当前基础上,该常量增加多少
技巧
比较时间
Date date = new Date(); // Fri Dec 30 17:03:12 CST 2022
long l = 1111133000000L;
Date date1 = new Date(l); // Fri Mar 18 16:03:20 CST 2005
// 比较,before 在之前, after 在之后
System.out.println(date.before(date1));
Executors 线程池
使用工具类创建线程池对象
// 使用工具类创建线程池对象
ExecutorService ex = Executors.newFixedThreadPool(3);
// 返回submit
Future<String> submit = ex.submit(new MyRunnable(100));
// 打印
System.out.println(submit.get());
//pool-1-thread-1执行结果是:5050
说明:介绍多个 |
---|
newFixedThreadPool(3) |
创建固定线程3个,如果某个线程因为执行异常而结束,那么会自动补充 |
newCachedThreadPool() |
线程数随着任务增加而增加,如果线程任务执行完毕且空闲了一段时间,会自动回收 |
newSingleThreadExecutor() |
创建只有一个线程的线程池,如果该线程出现异常,会自动补充新的 |
newScheduledThreadPool() |
创建只有一个线程的线程池,可以实现给定延迟后运行的任务,或者定期执行任务 |
大型开发环境不可以使用这些,会存在问题
存在的隐患 | |
---|---|
newFixedThreadPool | 允允许请求的任务队列长度是Integer.MAX_VALUE,可能出现OOM错误( java.lang.OutOfMemoryError ) |
newSingleThreadExecutor | |
newCachedThreadPool | 创建的线程数量最大上限是Integer.MAX_VALUE, 线程数可能会随着任务1:1增长,也可能出现OOM错误( java.lang.OutOfMemoryError ) |
newScheduledThreadPool |
Executors工具类底层是基于什么方式实现的线程池对象?
线程池ExecutorService的实现类:ThreadPoolExecutor
TimeUnit 时间段
返回时间段:
- NANOSECONDS:纳秒(=千分之一微妙)
- MICROSECONDS:微妙(=千分之一毫秒)
- MILLISECONDS:毫秒(=千分之一秒)
- SECONDS:秒
- MINUTES:分钟
- HOURS:小时
- DAYS:天
Math 数学
abs(绝对值)
System.out.println(Math.abs(-1));
absExact校验数值是是否越界
返回值为绝对值之后的数字
ceil(向上取整)
System.out.println(Math.ceil(0.6));
返回值是double取整之后的数字
floor(向下取整)
System.out.println(Math.floor(0.48));
返回值是double取整之后的数字
round(四舍五入)
System.out.println(Math.round(0.58));
返回值是int四舍五入之后的数字
sqrt(平方根)
System.out.println(Math.sqrt(16));
返回值是double开平方之后的数字
max,min(比较最大 / 最小)
// 比较最大
System.out.println(Math.max(16,5));
// 比较最小
System.out.println(Math.min(16,5));
返回值最大值 / 最小值
cbrt(立方根)
System.out.println(Math.cbrt(20));
返回值是开立方后的数字
pow(计算平方)
System.out.println(Math.pow(2,3));
返回值是2的3次方
random(随机生成0到1小数)
System.out.println(Math.random());
返回一个小数0~1
rint(四舍六入五留双)
System.out.println(Math.rint(17.5));
小数大于5,进位,否则判断前面整数是奇数还是偶数,奇数进位,偶数保留
String(字符串)
length(获取字符串长度)
String s = "abcdefg";
// 返回值为字符串长度
int l = s.length();
返回值为字符串长度
charAt(获取单个字符)
String s = "abcdefg";
// 获取第一个字符,返回值为当前索引的字符
char c = s.charAt(1);
System.out.println(c);
返回值为当前索引的字符
getBytes(转为byte数组)
String s = "abcdefg";
// 返回值为字节数组,把字符串转为byte字节数组
String s = "abcdefg";
byte[] bytes = s.getBytes();
// 遍历数组
for (byte aByte : bytes) {
System.out.println(aByte);
}
返回值为字节数组,把字符串转为byte字节
toLowerCase(转为小写字母)
String s = "ABCDEFG";
String s1 = s.toLowerCase();
System.out.println(s);
System.out.println(s1);
返回值为转为小写字母的字符串
toUpperCase(转为大写字母)
String s = "abcdefg";
String s1 = s.toUpperCase();
System.out.println(s);
System.out.println(s1);
返回值为转为大写字母的字符串
concat(字符串添加)不常用
String s = "abcdefg";
String concat = s.concat("1234");
System.out.println(concat);
返回值为添加完字符的字符串
contains(判断字符串是否包含该字符)
String s = "abcdefg";
// 如果包含 a1 就返回true
boolean a = s.contains("a1");
System.out.println(a);
判断是否包含该字符,返回值为 true 或者 false
startsWith(判断字符串是否以指定字符开头)
String s = "abcdefg";
boolean g = s.startsWith("1");
System.out.println(g);
判断是否以指定字符开头,返回值为 true 或者 false
endsWith(判断字符串是否以指定字符结尾)
String s = "abcdefg";
boolean g = s.endsWith("1");
System.out.println(g);
判断是否以指定字符结尾,返回值为 true 或者 false
indexOf(判断字符在字符串中的位置)
String s = "abcdefg";
// 判断a在字符串中的位置,第一次出现的位置
int a = s.indexOf("a");
System.out.println(a);
判断字符在字符串中的位置,返回-1表示搜索不到
用法:可传递一个int数值,表示在ascll的字符
第二个参数表示起始位置,从第几位开始搜索
lastindexOf(判断字符在字符串中的最后出现的位置)
同上
split(根据分割规则,返回字符串数组)
String s = "a,bc,de,fg";
// 根据 , 号分割
String[] split = s.split(",");
for (String s1 : split) {
System.out.println(s1);
}
根据分割规则,返回字符串数组,第二个参数是正则表达式
substring(切割字符串)
String s = "abcdefg";
// 从 0 索引开始获取,不包含1
String ss = s.substring(0, 1);
System.out.println(ss);
返回切割字符串,从0开始获取,不包含后面。
trim(去除两边的空格)
String s = " abc de fg ";
// 去除两边的空格
String trim = s.trim();
System.out.println(trim);
去除两边的空格,不包含中间的空格
equalsIgnoreCase(忽视大小写)
String s = "abcdefg";
String s1 = "ABCDEFG";
boolean b = s.equalsIgnoreCase(s1);
System.out.println(b);
判断两个字符串是否相等,忽视大小写
isEmpty(字符串是否为空)
String s = "abcdefg";
boolean empty = s.isEmpty();
System.out.println(empty);
判断字符串是否为空,返回Boolean