java中常用的方法集合Math、System、三代日期、Arrays数组的使用
Math类
常用方法:
- abs 绝对值
- pow 求幂
- ceil 向上取整
- floor 向下取整
- round 四舍五入
- sqrt 开方
- random 随机数
- max 求两数之间的最大值(注意是两个数,不要认为是所有的数量)
- min 求两数之间的最小值
random 随机数测试
- 规则:左闭右开 ,随机生成 [ n , m) 的数据
公式:n+ Math.random() *(m)
- 规则:左闭右开 ,随机生成 [ n , m ] 的数据
公式:n+ Math.random() *(m+1)
自定义n位保留小数 (DecimalFormat方法)
double d = 12.2314213;
DecimalFormat decimalFormat = new DecimalFormat("0.0");
String format = decimalFormat.format(d);
Array数组的使用
对于sort排序的理解(面向接口编程,自定义sort排序)
public class 冒泡 {
public static void main(String[] args) {
int [] arr = {12, 34, 3, 4, 5, 2, 6, 17, 33};
bubble(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1 = (Integer) o1;
int i2 = (Integer) o2;
return i2-i1;
}
});
for (int i: arr) {
System.out.print(i+" ");
}
}
// sort面向接口变成的界定因素就是c.compare(arr[j], arr[j+1])>0 当传入的参数大于0是冒泡排序,反之排序相反
public static void bubble(int [] arr , Comparator c) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (c.compare(arr[j], arr[j+1])>0) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
BinarySearch二分搜索的使用
int [] arr = {1,2,3,4,5,66};
int i = Arrays.binarySearch(arr, 67); // 如果不存在,返回的是应该在谁后面的index+1取负数
System.out.println(i); // 输出-7
- 如果查找的数不存在,返回的是负数,规则是:返回的是应该在谁后面的index+1取负数
copyOf复制数组
- 从 arr 数组中,拷贝 arr.length 个元素到 newArr 数组中
- 如果拷贝的长度 > arr.length 就在新数组的后面 增加 null
- 如果拷贝长度 < 0 就抛出异常 NegativeArraySizeException
- 该方法的底层使用的是 System.arraycopy()
Integer[] newArr = Arrays.copyOf(arr, arr.length)
数组的填充fill
int [] arr = {1,2,3,4,5,66};
Arrays.fill(arr,0,3,3);
判断两个数组是否完全相等:equals
将数组转化为集合asList
-
asList 方法,会将 arr 数组数据转成一个 List 集合
-
返回的 asList 编译类型 List(接口)
-
asList 运行类型 java.util.Arrays$ArrayList, 是 Arrays 类的 静态内部类 private static class ArrayList extends AbstractList implements RandomAccess, java.io.Serializable
int[] arr = {2,3,4,5,6,1}
List asList = Arrays.asList(arr)
System.out.println("asList 的运行类型" + asList.getClass()); // asList 的运行类型class java.util.Arrays$ArrayList
System类的常用方法
- exit 退出当前程序
- currentTimeMillens 返回当前时间距离1970-1-1的毫秒数
- gc:运行垃圾回收机制 System.gc();
极大整数bigInteger
BigInteger big = new BigInteger("1541284789123123941927");
BigInteger add = new BigInteger("123");
//加
BigInteger add1 = big.add(add);
// 减
BigInteger subtract = big.subtract(add);
// 乘
BigInteger multiply = big.multiply(add);
// 除
BigInteger divide = big.divide(add);
极大小数bigdecimal
- 加减乘和bigInteger一致,除法有一点特别,以为可能存在无限循环小数,或报错,需要指定
- BigDecimal.ROUND_CEILING 保留的小数和被除数一致
BigDecimal bd = new BigDecimal("12341234345235.0");
BigDecimal div = new BigDecimal("12.342134");
// BigDecimal.ROUND_CEILING 保留的小数和被除数一致
BigDecimal divide2 = bd.divide(div, BigDecimal.ROUND_CEILING);
System.out.println("结果"+divide2); // 结果: 999927107033.2
日期类
第一代
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss E");
Date date = new Date();
String format = sdf.format(date);
System.out.println(format);
第三代
- 提供的是LocalDateTime方法
- 单独获取年月日时分秒的方法:
LocalDateIem ld = new localDateTime();
"年=" + ld.getYear()
"月=" + ld.getMonth();
"月=" + ld.getMonthValue();
"日=" + ld.getDayOfMonth();
"时=" + ld.getHour();
"分=" + ld.getMinute));
"秒=" + ld.getSecond();\ - 提供 plus 和 minus 方法可以对当前时间进行加或者减;
// 年月日 时分秒,年月日,时分秒
LocalDateTime ld = LocalDateTime.now();
LocalDate ld1 = LocalDate.now();
LocalTime ld2 = LocalTime.now();
System.out.println(ld1+"\n"+ld2+"\n"+ld);
// 格式化
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss E");
String format1 = dtf.format(ld);
System.out.println(format1);
//看看 890 天后,是什么时候 把 年月日-时分秒
LocalDateTime localDateTime = ld.plusDays(890);
System.out.println("890 天后=" + dtf.format(localDateTime));
//看看在 3456 分钟前是什么时候,把 年月日-时分秒输出
LocalDateTime localDateTime2 = ld.minusMinutes(3456);
System.out.println("3456 分钟前 日期=" + dtf.format(localDateTime2));
第三代提供的方法
- LocalDateTime类
- 判断闰年 (isLeapYear()方法)
- 增加日期的某个部分
- plus增加时间的某个部分
- minus时间的减发
判断是否为闰年
LocalDateTime ldt = LocalDateTime.now();
localDateTime.toLocalDate().isLeapYear();
时间戳Instant与Date日期的转化
- 时间戳函数 Instant.now();
- 转Date Date from = Date.from(instant);
- Date转时间戳 Instant instant1 = from.toInstant();
测试----对数组的一个指定位置进行翻转
目的:将1-5索引为的字符进行翻转
输入:a1ssdfjioasjfi0
输出:afdss1jioasjfi0
public String reverse(int start, int end, String str) {
char[] chars = str.toCharArray();
for (int i = start,j=end; i <j ; i++,j--) {
char temp = chars[i];
chars[i]=chars[j];
chars[j]= temp;
}
return new String(chars);
}