Arrays
查看Apl,Arrays在util包下且Arrays的方法都是静态方法,所以可以直接使用Arrays调用静态方法使用,不需要创建它的对象
常用的Arrays静态方法:
Arrays.toString(数组参数);得到一个指定数组内容的字符串
int[] array={1,2,3,4};
String s = Arrays.toString(array);
System.out.println(s);//[1, 2, 3, 4]
Arrays.sort(数组参数),可以将其按照升序进行排序
char[] chars={'c','b','d','a'};
int[] ints={1,3,2,73,7};
Arrays.sort(chars);
Arrays.sort(ints);
System.out.println(Arrays.toString(chars));//[a, b, c, d]
System.out.println(Arrays.toString(ints));//[1, 2, 3, 7, 73]
其它Arrays静态方法查看Apl
Math
Math是一个关于数学的工具类,在lang包下,不需要导包,而且与Arrays类一样全是静态方法,直接使用类名调用
常用静态方法:
Math.PI 求圆周率
double pi=Math.PI;
System.out.println(pi);//3.141592653589793 圆周率
Math.abs(参数) 得到参数的绝对值
int i=Math.abs(-2);double d=Math.abs(-3.33);
System.out.println(i+" "+d);// 2 3.33 得到绝对值
public static double ceil(double a) 向上取值
public static double floor(double a)向下取值
double ceil = Math.ceil(4.33);
System.out.println(ceil);// 5.0 向上取大于它的整值
double floor = Math.floor(9.99);
System.out.println(floor);// 9.0 向下取小于它的整数值
public static int round(float a) 四舍五入
float float01=6.33F;
float float02=9.99F;
System.out.println(Math.round(float01));// 6 四舍五入
System.out.println(Math.round(float02));// 10 四舍五入
其它静态方法查看Apl。
练习代码:
/*
* 计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
* */
public class Math04 {
public static void main(String[] args) {
int count=0;
double a=-10.8;
double b=5.9;
int ceil = (int) Math.ceil(a);
int floor = (int)Math.floor(b);
for (int i = ceil; i < floor; i++) {
if (Math.abs(i)>6||Math.abs(i)<2.1){
count++;
System.out.println(i);
}
}
System.out.println("一个有多少个:"+count);
}
}