一、Arrays类
1 概述
java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等。其所有方法均为静态方法,调用起来非常简单。
2 操作数组的方法
public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
public static void main(String[] args) {
// 定义int 数组
int[] arr = {2,34,35,4,657,8,69,9};
// 打印数组,输出地址值
System.out.println(arr); // [I@2ac1fdc4
// 数组内容转为字符串
String s = Arrays.toString(arr);
// 打印字符串,输出内容
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9]
}
public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
public static void main(String[] args) {
// 定义int 数组
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2};
System.out.println("排序前:"+ Arrays.toString(arr));
// 排序前:[24, 7, 5, 48, 4, 46, 35, 11, 6,2]
// 升序排序
Arrays.sort(arr);
System.out.println("排序后:"+ Arrays.toString(arr));
// 排序后:[2, 4, 5, 6, 7, 11, 24, 35, 46,48]
}
二、 Math类
1 概述
java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单
2 基本运算的方法
public static double abs(double a) :返回 double 值的绝对值。
double a1 = Math.abs(‐4); //a1的值为4
double a2 = Math.abs(4); //a2的值为4
public static double ceil(double a) :返回大于等于参数的最小的整数。
ouble a1 = Math.ceil(3.3); //a1的值为 4.0
double a2 = Math.ceil(‐3.3); //a2的值为 ‐3.0
double a3 = Math.ceil(5.1); //a3的值为 6.0
public static double floor(double a) :返回小于等于参数最大的整数。
double a1 = Math.floor(3.3); //a1的值为3.0
double a2 = Math.floor(‐3.3); //a2的值为‐4.0
double a3 = Math.floor(5.1); //a3的值为 5.0
public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
long a1 = Math.round(5.5); //a1的值为6.0
long a2 = Math.round(5.4); //a2的值为5.0