Java 常用工具类详解(数学、数组)

在 Java 中,工具类是专门为特定场景设计的类,通常包含大量静态方法,方便开发者直接使用。本文将介绍 Java 中的两个重要工具类:数学工具类 Math 和数组工具类 Arrays

1. 数学工具类(Math)

Java 提供的运算符主要用于基本算术运算,而 Math 类则扩展了更多高级数学运算,如指数、对数、三角函数等。Math 属于 java.lang 包,不需要额外导入,可以直接使用。

1.1 常见数学运算

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.pow(5, 3)); // 5^3
        System.out.println(Math.abs(-1)); // 绝对值
        System.out.println(Math.max(19, 20)); // 最大值
        System.out.println(Math.min(2, 4)); // 最小值
        System.out.println(Math.sqrt(9)); // 平方根
    }
}

1.2 三角函数

System.out.println(Math.sin(Math.PI / 2)); // sin(π/2)
System.out.println(Math.cos(Math.PI)); // cos(π)
System.out.println(Math.tan(Math.PI / 4)); // tan(π/4)
System.out.println(Math.asin(1)); // arcsin(1)
System.out.println(Math.acos(1)); // arccos(1)
System.out.println(Math.atan(0)); // arctan(0)

⚠️ 注意:由于浮点数的精度问题,某些计算结果可能略有偏差,如 Math.sin(Math.PI) 可能不会完全等于 0。

1.3 对数运算

System.out.println(Math.log(Math.E)); // ln(e) = 1
System.out.println(Math.log10(100)); // log10(100) = 2
System.out.println(Math.log(4) / Math.log(2)); // log2(4) = 2

1.4 取整操作

System.out.println(Math.ceil(4.5)); // 向上取整 → 5
System.out.println(Math.floor(5.6)); // 向下取整 → 5

⚠️ 对负数:ceil(-4.5) → -4floor(-4.5) → -5

1.5 随机数生成

import java.util.Random;
public class RandomDemo {
    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            System.out.println(random.nextInt(100)); // 生成 0-99 之间的随机整数
        }
    }
}

⚠️ Java 的随机数是伪随机的,基于数学计算得出。


2. 数组工具类(Arrays)

Arrays 类是 java.util 包下的工具类,提供了便捷的方法操作数组。

2.1 数组打印

import java.util.Arrays;
public class ArraysDemo {
    public static void main(String[] args) {
        int[] arr = {1, 4, 5, 8, 2, 0, 9, 7, 3, 6};
        System.out.println(Arrays.toString(arr));
    }
}

2.2 数组排序

Arrays.sort(arr); // 升序排序
System.out.println(Arrays.toString(arr));

2.3 数组填充

int[] arr = new int[5];
Arrays.fill(arr, 66);
System.out.println(Arrays.toString(arr));

2.4 数组拷贝

int[] arr = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(arr, 5); // 复制整个数组
System.out.println(Arrays.toString(copy));
int[] copyRange = Arrays.copyOfRange(arr, 1, 4); // 复制索引 1-3
System.out.println(Arrays.toString(copyRange));
int[] target = new int[10];
System.arraycopy(arr, 0, target, 0, 5); // 部分复制
System.out.println(Arrays.toString(target));

2.5 二分查找(仅适用于有序数组)

int[] sortedArr = {1, 2, 3, 4, 5};
System.out.println(Arrays.binarySearch(sortedArr, 3)); // 返回索引 2

2.6 多维数组操作

int[][] array = {
   
   {2, 8, 4, 1}, {9, 2, 0, 3}};
System.out.println(Arrays.deepToString(array)); // 打印多维数组

2.7 数组比较

int[][] a = {
   
   {2, 8, 4, 1}, {9, 2, 0, 3}};
int[][] b = {
   
   {2, 8, 4, 1}, {9, 2, 0, 3}};
System.out.println(Arrays.equals(a, b)); // false
System.out.println(Arrays.deepEquals(a, b)); // true

⚠️ Arrays.equals() 只适用于一维数组,而 Arrays.deepEquals() 适用于多维数组。


3. 总结

  • Math 类提供高级数学计算方法,如三角函数、对数、随机数等。
  • Arrays 类简化了数组操作,包括排序、填充、拷贝、二分查找等。
  • Arrays.deepToString()Arrays.deepEquals() 适用于多维数组。

这些工具类大大提高了 Java 开发的效率,是日常编程中不可或缺的工具!