Java基礎之:Math & Arrays & System
阿新 • • 發佈:2020-12-22
Math
Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函式。
方法介紹:
1.abs 絕對值
2.pow 求冪
3.ceil 向上取整,返回>=該引數的最小整數;
4.floor 向下取整,返回<=該引數的最大整數
5.round 四捨五入 Math.floor(該引數+0.5)
6.sqrt 求開方
7.random 返回隨機數【0——1) ,重點!!
package class_Math; public class ClassTest01 { public static void main(String[] args) { //1.abs 絕對值 int abs = Math.abs(9); System.out.println(abs); //2.pow 求冪 double pow = Math.pow(-3.5, 4); System.out.println(pow); //3.ceil 向上取整,返回>=該引數的最小整數; double ceil = Math.ceil(-3.0001); System.out.println(ceil); //4.floor 向下取整,返回<=該引數的最大整數 double floor = Math.floor(-4.999); System.out.println(floor); //5.round 四捨五入 Math.floor(該引數+0.5) long round = Math.round(-5.001); System.out.println(round); //6.sqrt 求開方 double sqrt = Math.sqrt(-9.0); System.out.println(sqrt); //7.random 返回隨機數【0——1) //[a-b]:int num = (int)(Math.random()*(b-a+1)+a) double random = Math.random(); System.out.println(random); //小技巧:獲取一個 a-b 之間的一個隨機整數 int a = (int)(Math.random()*(15-7+1)+7); System.out.println(a); /* * 理解: * 1.Math.random() 是 [0,1)的隨機數 * 2.(Math.random()*(15-7+1) 就是[0,9) * 3.Math.random()*(15-7+1)+7 就是[7,16) * 4.(int)取整就是 [7,15] ,即[a,b]之間的隨機整數 */ } }
Arrays
Arrays裡面包含了一系列靜態方法,用於管理或運算元組(比如排序和搜尋)。
Arrays排序sort方法
package class_Arrays; import java.util.Arrays; import java.util.Comparator; public class ClassTest01 { public static void main(String[] args) { Integer[] arr = { 25, 35, 11, 32, 98, 22 }; // Arrays.sort(arr); //預設小到大排序 System.out.println(Arrays.toString(arr)); // 使用匿名內部類重寫compare方法,實現從大到小排序 Arrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { if (o1 > o2) { return -1; } else if (o1 < o2) { return 1; } else { return 0; } } }); System.out.println(Arrays.toString(arr)); } } //自寫一個Arrays中的sort方法 體會為什麼通過重寫之後,匿名內部類動態繫結機制改變了排序方式 class MyArrays { @SuppressWarnings("unchecked") public static void sort(Integer[] arr,Comparator c) { Integer temp = 0;// 自動裝箱 for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (c.compare(arr[j] , arr[j + 1]) > 0) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public static void main(String[] args) { Integer[] arr = { 25, 35, 11, 32, 98, 22 }; // MyArrays.sort(arr);//不新增 Comparator介面物件為引數,就是簡單的氣泡排序方法 //新增匿名內部類物件為引數,就可以改變排序方式。用此方法可以很靈活的使用排序。 MyArrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //通過返回值的正負來控制,升序還是降序 //只有當返回值為1時,才會發生交換。例如這裡,o1 < o2時返回1 ,進行交換 //也就是需要前面的數,比後面的數大,即降序 if (o1 > o2) { return -1; } else if (o1 < o2) { return 1; } else { return 0; } } }); System.out.println(Arrays.toString(arr)); } }
Arrays查詢binarySearch方法及其他常用方法
package class_Arrays; import java.util.List; import java.util.Arrays; public class ClassTest02 { public static void main(String[] args) { //隨機生成陣列,並排序 Integer[] arr = new Integer[10]; // for (int i = 0; i < arr.length; i++) { // arr[i] = (int)(Math.random()*100) + 1 ; // } // Arrays.sort(arr); arr = new Integer[]{16, 28, 41, 51, 62, 67, 67, 86, 90, 93}; System.out.println(Arrays.toString(arr)); // binarySearch 通過二分搜尋法進行查詢,要求必須排好序 int index = Arrays.binarySearch(arr, 55); //返回 -5 (55在51和62之間) //找到則返回下標,若沒有找到則返回-(low + 1)。即此數在陣列中應該在的位置的下標 + 1,例: //{1,3,5,9,10},此陣列中找2,沒有找到則返回-2,因為2本來在1和3的中間。下標為1再+1,就是-2 System.out.println(index); // copyOf 陣列元素的複製,引數列表:目標陣列,需拷貝元素個數(若超過,使用null填充,若<0,報錯) Integer[] newArr = Arrays.copyOf(arr, arr.length - 5); Integer[] newArr2 = Arrays.copyOf(arr, arr.length + 5); System.out.println(Arrays.toString(newArr)); System.out.println(Arrays.toString(newArr2)); // fill 陣列元素的填充,將陣列中的所有元素填充為所指定的內容 Integer[] num = new Integer[] { 9, 3, 2 }; Arrays.fill(num, 99); System.out.println(Arrays.toString(num)); //equals 比較兩個陣列元素內容是否完全一致 Integer[] array = new Integer[]{1,2,3}; Integer[] array2 = new Integer[]{1,2,3}; boolean equals = Arrays.equals(array, array2); System.out.println(equals); //asList 將一組值,轉換成list List<Integer> asList = Arrays.asList(2,3,4,5,6,1); System.out.println("asList=" + asList); } }
Arrays類練習題
案例:自定義Book類,裡面包含name和price,按price排序(從大到小)。要求使用兩種方式排序 , 對物件的某個屬性排序, 有一個 Book[] books = 3本書物件. 方式1:使用前面學習過的傳遞 實現Comparator介面匿名內部類,也稱為定製排序。 方式2:讓Book實現Comparable介面,完成排序。
package class_Arrays; import java.util.Arrays; import java.util.Comparator; /* 案例:自定義Book類,裡面包含name和price,按price排序(從大到小)。要求使用兩種方式排序 , 對物件的某個屬性排序, 有一個 Book[] books = 3本書物件. 方式1:使用前面學習過的傳遞 實現Comparator介面匿名內部類,也稱為定製排序。 方式2:讓Book實現Comparable介面,完成排序。 */ public class ClassWork01 { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { Book[] books = {new Book("三體1",30.2),new Book("三體2",28.2),new Book("三體3",29.2)}; System.out.println(Arrays.toString(books)); System.out.println("============================="); // Arrays.sort(books, new Comparator() { // @Override // public int compare(Object o1, Object o2) { // // Book b1 = (Book)o1; // Book b2 = (Book)o2; // if(b1.getPrice() > b2.getPrice()) { // return 1; // }else if (b1.getPrice() < b2.getPrice()) { // return -1; // }else { // return 0; // } // } // }); Arrays.sort(books); System.out.println(Arrays.toString(books)); } } class Book implements Comparable<Book>{ private String name; private double price; public Book(String name, double price) { super(); this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book [name=" + name + ", price=" + price + "]"; } @Override public int compareTo(Book o) { if(this == o) { return 0; } if(!(o instanceof Book)) { return 0; } double price = ((Book)o).price; if(this.price > price ){ return -1; } else if(this.price < price) { return 1; } else { return 0; } } }
System
-
exit 退出當前程式
-
arraycopy :複製陣列元素,比較適合底層呼叫,一般使用Arrays.copyOf完成複製陣列. int[] src={1,2,3}; int[] dest = new int[3]; System.arraycopy(src, 0, dest, 0, 3);
-
currentTimeMillens:返回當前時間距離1970-1-1 的毫秒數
-
gc:執行垃圾回收機制 System.gc();
import java.util.Arrays; public class System_ { public static void main(String[] args) { // - // System.out.println("hello, world~~"); // System.exit(0); //退出程式 // // System.out.println("hello, world~~"); // arraycopy :複製陣列元素,比較適合底層呼叫,一般使用Arrays.copyOf完成複製陣列. int[] src = { 1, 2, 3 }; //源陣列 int[] dest = new int[3];//目標陣列 //解讀 //1. src源陣列 //2. 0 從src 的哪個索引開始拷貝 //3. dest 目標陣列 //4. 0 表示把元素拷貝 到 dest第幾個索引後 //5. 3 拷貝幾個元素 System.arraycopy(src, 1, dest, 1, 2); System.out.print(Arrays.toString(dest)); } }