Array
Array 含有sort、fill、equals、BinarySearch等方法
import java.util.Arrays;
import java.util.Random;
public class ArraysDemo{
public static void main(String[] args){
int[] arr = new int[10];
Arrays.fill(arr, 0);
System.out.println("fill with 0 : " + Arrays.toString(arr));
Random rand = new Random();
for (int i = 0; i < arr.length; i++){
arr[i] = rand.nextInt(101);
}
System.out.println("Refill with random : " + Arrays.toString(arr));
arr[5] = 50;
Arrays.sort(arr);
System.out.println("Sort : " + Arrays.toString(arr));
int ind = Arrays.binarySearch(arr, 50);
System.out.println("Index of 50 : " + ind);
int[] newArr = Arrays.copyOf(arr, arr.length);
System.out.println("Copy right ? : " + Arrays.equals(arr, newArr));
}
}
輸出結果:
fill with 0 : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Refill with random : [13, 84, 8, 98, 79, 76, 73, 30, 31, 41]
Sort : [8, 13, 30, 31, 41, 50, 73, 79, 84, 98]
Index of 50 : 5
Copy right ? : true
注意:binarySearch只能用於已經排序的數組。如返回正數,爲所在位置。如果找不到,則爲負數,其絕對值表示這個數應該被插入的位置。
StringBuilder
StringBuilder是一個可以insert、delete、replace、append字符串中元素的一個可變的對象。內置了很多字符串操作的相關方法。StringBuilder可以通過toString方法轉爲String類型。下面是一個例子:
public class StringBuilderTest{
public static void main(String[] args){
StringBuilder s = new StringBuilder("I");
s.append(" ML");
s.insert(1, " love");
String t = s.toString();
System.out.println(t);
}
}
輸出爲:
I love ML
Math
Math在java.lang中,實現了很多常用的數學函數,比如sin、cos等三角、反三角函數,以及ceil、floor、rint等不同的double取整方式,以及pow計算冪,以及random生成隨機數等。
通過一個例子,來使用一下Math中的常見函數:
import java.lang.Math;
public class MathDemo{
public static void main(String[] args){
System.out.println(Math.ceil(6.66));
System.out.println(Math.floor(6.66));
System.out.println(Math.rint(6.66));
System.out.println(Math.round(6.66));
System.out.println(Math.abs(6.66));
System.out.println(Math.sin(6.66));
System.out.println(Math.atan(6.66));
System.out.println(Math.pow(6.66, 2));
System.out.println(Math.sqrt(6.66));
}
}
輸出結果:
7.0
6.0
7.0
7
6.66
0.36796051057238466
1.4217595363083344
44.3556
2.5806975801127883
注意:rint和round的區別在於,rint返回的仍是double,而round對於double返回的是long,對float返回int。都是返回最接近的整數值。
實驗,random兩個數,比較大小:
import java.lang.Math;
public class MathTest{
public static void main(String[] args){
double a = Math.random();
double b = Math.random();
double max = Math.max(a, b);
System.out.println("random a is : " + a);
System.out.println("random b is : " + b);
System.out.println("maximum is : " + max);
}
}
輸出結果:
random a is : 0.5640893547983834
random b is : 0.4307469193802972
maximum is : 0.5640893547983834
System
System不能被實例化,只能訪問static方法。常用的System.out.println就是System 的一個方法。
實驗案例:
import java.util.Arrays;
public class SystemDemo{
public static void main(String[] args){
int[] a = {1, 2, 3, 4, 5};
int[] b = {6, 7, 8, 9, 10};
System.arraycopy(a, 1, b, 2, 3);
System.out.println(Arrays.toString(b));
System.out.println("Java Version is :" + System.getProperty("java.version"));
}
}
輸出結果:
[6, 7, 2, 3, 4]
Java Version is :1.8.0_131
Random
實驗:輸入一個m和一個n,其中m小於n,兩者都是正整數,輸出一個[m, n]範圍內的隨機數。
import java.util.Random;
import java.util.Scanner;
public class RandomTest{
public static void main(String[] args){
Scanner inp = new Scanner(System.in);
int m = inp.nextInt();
int n = inp.nextInt();
Random rand = new Random();
int ret = rand.nextInt(n - m + 1) + m;
System.out.println(ret);
}
}
輸入輸出爲:
20
53
39
2020年7月8日00:58:49
北京 生命科學園