Arrays类
java.util.Arrays is a utility class with an array of relevant, which provides a number of static methods used to implement an array of common operations.
public static String toString (array): The parameter array into a string (in accordance with the default format: [element 1, element 2, element 3 ...])
public static void main(String[] args) {
int[] intArray = {10, 20, 30};
//将int[]数组按照默认格式变成字符串
String intStr = Arrays.toString(intArray);
System.out.println(intStr);//[10, 20, 30]
//以下出了啥问题(报错)
// Arrays arrays = new Arrays();
// String str = arrays.toString(intArray);
// System.out.println(str);
}
public static void sort (array): ascending order by default (small to large) to sort the elements of the array.
int[] array1 = {1, 3, 5, 4, 9, 3};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));//[1, 3, 3, 4, 5, 9]
String[] array2 = {"bbb", "aaa", "ccc"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));//[aaa, bbb, ccc]
Remarks:
- If the value is, sort in ascending order from small to large default.
- If a character, sort default in alphabetical order.
- If it is a custom type, this custom class needs the support Comparable or Comparator interface. (Future learning)
Arrays Exercise:
1. Use API Arrays related to all the characters in the ascending order of the arrangement of a random string, and reverse printing.
public static void main(String[] args) {
String str = "kfnvkjnkjbkjb159ejbeb";
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
for (int i = charArray.length - 1; i >= 0; i--) {
System.out.print(charArray[i]);//vnnkkkkjjjjfeebbbb951
}
}
Mathematics Math Tools
java.util.Math is a mathematics-related tools, which provides a number of static methods, to complete the operation related to mathematical operations.
public static double abs (double num): obtaining an absolute value. There are many overloaded.
public static void main(String[] args) {
//获取绝对值
System.out.println(Math.abs(3.14));//3.14
System.out.println(Math.abs(0));//0
System.out.println(Math.abs(-2.5));//2.5
}
public static double ceil(double num):向上取整。
//向上取整
System.out.println(Math.ceil(3.9));//4.0
System.out.println(Math.ceil(3.1));//4.0
System.out.println(Math.ceil(3.0));//3.0
public static double floor (double num): rounded down.
//向下取整
System.out.println(Math.floor(3.1));//3.0
System.out.println(Math.floor(3.9));//3.0
System.out.println(Math.floor(3.0));//3.0
public static long round (double num): rounding.
//四舍五入
System.out.println(Math.round(20.4));//20
System.out.println(Math.round(20.5));//21
Representative Math.PI approximate constant pi (double).
Math Exercise:
1. Compute between -10.8 to 5.9, the absolute value of greater than 6 or less than 2.1 have an integer number?
public static void main(String[] args) {
int count = 0;
double min = -10.8;
double max = 5.9;
//这样处理,变量i就是区间之内所有的整数
for (int i = (int)min; i < max; i++) {
int abs = Math.abs(i);
if (abs > 6 || abs < 2.1) {
System.out.print(abs + " ");
count ++;
}
}
System.out.println("总个数有:" + count);
}