Arrays工具类和数学工具类Math
Arrays数组工具类
这个一个静态方法是用于操作数组的而且不需要生成对象就可以使用
Arrays里面的内容
- toString()方法().返回值类型是String
- sort()方法
代码示例
import java.sql.SQLOutput;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int a[]={1,3,4,6,8,9,4};
String str= Arrays.toString(a);//toString方法
System.out.println(str);
String s[]={"RRR","BBB","CCC","WWW"};
System.out.println(Arrays.toString(s));
//下面是排序方法
Arrays.sort(s);
Arrays.sort(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(s));
}
}
一个小练习
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
String str = "asdkfhiewoafilewkfoasujldhoe14991073";
char[] chars = str.toCharArray();
Arrays.sort(chars);
for (int i = chars.length - 1; i >= 0; i--) {
System.out.println(chars[i]);
}
}
}
Math工具类
- abs()绝对值
- ceil()向上取整
- floor()向下取整
- round()四舍五入
- 精确PI值
代码示例
public class Demo02 {
public static void main(String[] args) {
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(-3.14));
System.out.println(Math.abs(3));
System.out.println(Math.abs(-4));
System.out.println("================");
System.out.println(Math.ceil(3.01));
System.out.println(Math.ceil(3.9));
System.out.println(Math.ceil(3.0));
System.out.println(Math.ceil(3.1));
System.out.println("================");
System.out.println(Math.floor(3.14));
System.out.println(Math.floor(4.9));
System.out.println(Math.floor(3.5));
System.out.println(Math.floor(-4.6));
System.out.println("================");
System.out.println(Math.round(3.5));//返回值类型是long,上面的都是double
System.out.println(Math.round(4.4));
System.out.println(Math.round(-3.2));
System.out.println(Math.round(-4.6));
System.out.println("================");
System.out.println(Math.PI);
}
}
小学数学真题
题目要求
代码示例
public class Demo03 {
public static void main(String[] args) {
int num = 0;
double min = -10.8;
double max = 2.1;
for (int i = (int) Math.ceil(min); i <= (int) max; i++) {
int x = Math.abs(i);
if (x > 6 || x < 2.1) {
num++;
}
}
System.out.println("满足条件的数字有:" + num + "个");
}
}
标签:String,Arrays,System,println,工具,Math,out
From: https://www.cnblogs.com/harper886/p/17358173.html