Arrays类中的toString和sort方法
public class Demo01Arrays {
public static void main(String[] args) {
int[] intArray = {10, 20, 30};
String intStr = Arrays.toString(intArray);
System.out.println(intStr);
int[] array1 = {2, 1, 3, 10, 6};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
String[] array2 = {"bbb", "aaa", "ccc"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));
}
}
小练习1
import java.util.Arrays;
public class Demo02ArraysPractise {
public static void main(String[] args) {
String str = "asv76agfqwdfvasdfvjh";
char[] chars = str.toCharArray();
Arrays.sort(chars);
for (int i = chars.length - 1; i >= 0; i--) {
System.out.println(chars[i]);
}
}
}
Math类常用静态方法
- public static double abs(double num):获取绝对值。有多种重载。
- public static double ceil(double num):向上取整。
- public static double floor(double num):向下取整。
- public static long round(double num):四舍五入。
- Math.PI代表近似的圆周率常量(double)。
public class Demo03Math {
public static void main(String[] args) {
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(0));
System.out.println(Math.abs(-2.5));
System.out.println("================");
System.out.println(Math.ceil(3.9));
System.out.println(Math.ceil(3.1));
System.out.println(Math.ceil(3.0));
System.out.println("================");
System.out.println(Math.floor(30.1));
System.out.println(Math.floor(30.9));
System.out.println(Math.floor(31.0));
System.out.println("================");
System.out.println(Math.round(20.4));
System.out.println(Math.round(10.5));
}
}
小练习2
public class Demo04MathPractise {
public static void main(String[] args) {
int count = 0;
double min = -10.8;
double max = 5.9;
for (int i = (int) min; i < max; i++) {
int abs = Math.abs(i);
if (abs > 6 || abs < 2.1) {
System.out.println(i);
count++;
}
}
System.out.println("总共有:" + count);
}
}