常用类
常用类
1.包装类
int和Integer转换示例:
package com.commonclass;
/**
* Created on 2021/6/20.
*
* @author Ryan_小王
*/
public class Test {
public static void main(String[] args) {
//jdk5以前
//手动装箱(int -> Integer )
int n = 10;
Integer integer = new Integer(n);
Integer integer1 = Integer.valueOf(n);
//手动拆箱(Integer -> int)
int i = integer.intValue();
//jdk5以后,就可以自动装箱和自动拆箱
//自动装箱 int -> Integer
int n2 = 10;
Integer integer2 = n2; //底层使用的是Integer.valueof(n2)
//自动拆箱 Integer -> int
int i1 = integer2; //底层仍然使用的是intValue()方法
}
}
练习题:
包装类方法
以Integer和String转换为例
Integer和Character类常用方法
练习题
Integer.valueOf()源码
注意:只要有基本数据类型判断的就是值是否相等
2.Math类
1.基本介绍
Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。方法均为静态方法。
3.Arrays
Arrays类常用方法:
1.Arrays.sort()和Arrays.toString()
Integer[] integer = {2, 3, 1,6,8,20,7};
System.out.println(Arrays.toString(integer));
//升序为return o1-o2,因为若(o1-o2)返回的值小于0,类比成false;表示不想调整顺序,此时o2大于o1。
//降序为return o2-o1,因为若(o2-o1)返回的值小于0,类比成false,表示不想调整顺序,此时o2小于o1。
Arrays.sort(integer, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
System.out.println(Arrays.toString(integer));
//升序为return o1-o2,因为若(o1-o2)返回的值小于0,类比成false;表示不想调整顺序,此时o2大于o1。
//降序为return o2-o1,因为若(o2-o1)返回的值小于0,类比成false,表示不想调整顺序,此时o2小于o1。
实现定制版的冒泡+排序,匿名内部类实现Comparator接口完成定制。
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created on 2021/6/18.
*
* @author Ryan_小王
*/
public class Test {
public static void main(String[] args) {
int[] arr = {2, 3, 1, 6, 8, 20, 7};
//原始冒泡
// bubble01(arr);
// System.out.println(Arrays.toString(arr));
//冒泡+定制
bubble02(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1 =(Integer) o1;
int i2= (Integer) o2;
return i2 - i1;
}
});
System.out.println(Arrays.toString(arr));
}
//冒泡
public static void bubble01(int[] arr) {
int temp = 0;
//每次比较确定一个最大的值,所以要进行arr.length-1次
for (int i=0; i < arr.length - 1; i++) {
//每次比较的次数为arr.length - i - 1
for (int j=0; j < arr.length - i - 1; j++) {
temp = arr[j];
if (arr[j] > arr[j+1]) {
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//冒泡+定制
public static void bubble02(int[] arr, Comparator c) {
int temp = 0;
//每次比较确定一个最大的值,所以要进行arr.length-1次
for (int i=0; i < arr.length - 1; i++) {
//每次比较的次数为arr.length - i - 1
for (int j=0; j < arr.length - i - 1; j++) {
temp = arr[j];
if (c.compare(arr[j], arr[j+1]) > 0) {
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
2.Arrays.binarySearch()
1.要求数组有序,如果无序不能使用binarySearch
2.如果数组中不存在该元素,就返回return -(low + 1),即本应该在的位置加1然后取负号。
package com;
import java.util.Arrays;
/**
* Created on 2021/6/22.
*
* @author Ryan_小王
*/
public class Arrays01 {
public static void main(String[] args) {
//binarySearch通过二分搜索法进行查找,要求必须排好
//1.要求有序,如果无序不能使用binarySearch
//2.如果数组中不存在该元素,就返回return -(low + 1),即本应该在的位置加1然后取负号
int[] arr = {1, 2, 4, 5};
int index = Arrays.binarySearch(arr,3);
System.out.println(index);
}
}
3.Arrays.copyOf()
作用:按照规定的大小复制原始数组,超出的大小填充null。
Integer[] arr = {1, 2, 4, 5};
Integer[] newarr = Arrays.copyOf(arr,9);
System.out.println(Arrays.toString(newarr)); //[1,2],newLength大于arr则填充null
4.Arrays.fill();
作用:替换数组原始值。
Integer[] num = new Integer[]{1, 2, 9};
Arrays.fill(num,3);
System.out.println(Arrays.toString(num)); //[3, 3, 3],替换数组原始值
5.Arrays.equals()
作用:判断两个数组内容是否完全相同。
Integer[] arr1 = {1, 2};
Integer[] arr2 = new Integer[]{1, 2};
boolean equals = Arrays.equals(arr1, arr2);
System.out.println(equals);
6.Arrays.asList()
作用:将数据转换成一个List集合。
List<Integer> asList = Arrays.asList(2, 3);
System.out.println("asList=" + asList);
7.Arrays.sort()排序练习
package com;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created on 2021/6/22.
*
* @author Ryan_小王
*/
public class ArraysExercise {
public static void main(String[] args) {
Book[] book = new Book[4];
book[0] = new Book("文化苦旅", 45);
book[1] = new Book("小王子", 32);
book[2] = new Book("陪安东尼度过的漫长岁月", 40);
book[3] = new Book("追风筝的人", 55);
//(1)price从小到大
Arrays.sort(book, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
double priceVal = o1.getPrice() - o2.getPrice();
if (priceVal > 0) {
return -1;
} else if (priceVal < 0) {
return 1;
} else {
return 0;
}
}
});
//(2)书名长度从小到大
Arrays.sort(book, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
return o1.getName().length() - o2.getName().length();
}
});
System.out.println(Arrays.toString(book));
}
}
class Book {
private String name;
private double price;
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
4.String类
String、StringBuffer、StringBuilder介绍
5.System类
System类的常用方法
第二点举例:
6.BigInteger和BigDecimal类
当数很大的时候,long不够用,可以使用BigInteger类,底层是先将数据当作字符串,处理完成后转为BigInteger。
当数很大的时候,double不够用,可以使用BigDecimal类,底层是先将数据当作字符串,处理完成后转为BigDecimal。
1.BigInteger进行加减乘除需要用对应的方法。
2.BigDecimal进行加减乘除需要用对应的方法。
divide可能会抛异常,因为可能除不尽,使用BigDecimal.ROUND_CEILING就会保留分子的精度,就不会报异常。
7.日期类(Date、Calendar、LocalDate)
1.Date
//1.格式化输出Date
Date d1 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
String format = sdf.format(d1);
System.out.println(format); //2021年06月22日 10:49:39 星期二
//2.String -> Date
String s = "2021年06月22日 10:44:37 星期二";
Date parse = sdf.parse(s);
System.out.println(parse); //Tue Jun 22 10:44:37 CST 2021
2.Calendar
1.Calendar是一个抽象类,并且构造器是private
2.可以通过getInstance()来获取实例
3.提供大量的方法和字段给程序员
4.Calendar没有专门的格式化方法,需要自己组合
//1.Calendar是一个抽象类,并且构造器是private
//2.可以通过getInstance()来获取实例
//3.提供大量的方法和字段给程序员
Calendar c = Calendar.getInstance(); //创建日历类对象
System.out.println(c);
//2.获取日历对象的某个日历字段
System.out.println("年:" + c.get(Calendar.YEAR));
System.out.println("月:" + c.get(Calendar.MONDAY));
System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH)+1);
System.out.println("小时:" + c.get(Calendar.HOUR));
System.out.println("分钟:" + c.get(Calendar.MINUTE));
System.out.println("秒:" + c.get(Calendar.SECOND));
//Calendar没有专门的格式化方法,需要自己组合
System.out.println(c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月");
3.第三代日期类
//第三代日期
LocalDateTime now = LocalDateTime.now();
System.out.println("年:" + now.getYear());
System.out.println("月:" + now.getMonth());
System.out.println("日:" + now.getDayOfMonth());
System.out.println("小时:" + now.getHour());
System.out.println("分钟:" + now.getMinute());
System.out.println("秒:" + now.getSecond());
//使用DateTimeFormatter格式日期类
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh时mm分ss秒");
String format = dateTimeFormatter.format(now);
System.out.println("格式化日期=" + format);
//多少天后的日期
LocalDateTime localDateTime = now.plusDays(100);
System.out.println(dateTimeFormatter.format(localDateTime));