Java基礎學習筆記之:System類;Math類;Arrays類BigInteger,BigDecimal

System類

在API中System類介紹的比較簡單,咱們給出定義,System中表明程序所在系統,提供了對應的一些系統屬性信息,和系統操做。
System類不能手動建立對象,由於構造方法被private修飾,阻止外界建立對象。System類中的都是static方法,類名訪問便可。在JDK中,有許多這樣的類。
經常使用方法java

currentTimeMillis() 獲取當前系統時間與1970年01月01日00:00點之間的毫秒差值數組

exit(int status) 用來結束正在運行的Java程序。參數傳入一個數字便可。一般傳入0記爲正常狀態,其餘爲異常狀態dom

gc() 用來運行JVM中的垃圾回收器,完成內存中垃圾的清除。ide

getProperty(String key) 用來獲取指定鍵(字符串名稱)中所記錄的系統屬性信息函數

arraycopy方法,用來實現將源數組部分元素複製到目標數組的指定位置工具

System類的方法練習

驗證for循環打印數字1-9999所須要使用的時間(毫秒)大數據

複製代碼
public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i=0; i<10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗時毫秒:" + (end-start) );
    }
複製代碼

練習二:將src數組中前3個元素,複製到dest數組的前3個位置上spa

複製元素前:src數組元素[1,2,3,4,5],dest數組元素[6,7,8,9,10]
複製元素後:src數組元素[1,2,3,4,5],dest數組元素[1,2,3,9,10]指針

複製代碼
 public static void main(String[] args) {
        int[] src = new int[]{1,2,3,4,5};
        int[] dest = new int[]{6,7,8,9,10};
        System.arraycopy( src, 0, dest, 0, 3);
        //代碼運行後:兩個數組中的元素髮生了變化

        //src數組元素[1,2,3,4,5]
        //dest數組元素[1,2,3,9,10]
    }
複製代碼

練習三:循環生成100-999之間的的三位數並進行打印該數,當該數能被10整除時,結束運行的程序code

複製代碼
 public static void main(String[] args){
        Random random = new Random();
        while(true){
            int number = random.nextInt(900)+100; //0-899 + 100
            if (nmumber % 10 == 0) {
                System.exit(0);
            }
        }
    }
複製代碼

Math類

Math 類是包含用於執行基本數學運算的方法的數學工具類,如初等指數、對數、平方根和三角函數。
相似這樣的工具類[工具類,表明可以完成一系列功能的類,在使用它們時,不用建立對象,該類中方法爲靜態方法],其全部方法均爲靜態方法,而且通常不會建立對象。如System類

經常使用方法

abs方法,結果都爲正數

double d1 = Math.abs(-5); // d1的值爲5
double d2 = Math.abs(5); // d2的值爲5

ceil方法,結果爲比參數值大的最小整數的double值

double d1 = Math.ceil(3.3); //d1的值爲 4.0
double d2 = Math.ceil(-3.3); //d2的值爲 -3.0
double d3 = Math.ceil(5.1); // d3的值爲 6.0

floor方法,結果爲比參數值小的最大整數的double值

double d1 = Math.floor(3.3); //d1的值爲3.0
double d2 = Math.floor(-3.3); //d2的值爲-4.0
double d3 = Math.floor(5.1); //d3的值爲 5.0

max方法,返回兩個參數值中較大的值

double d1 = Math.max(3.3, 5.5); //d1的值爲5.5
double d2 = Math.max(-3.3, -5.5); //d2的值爲-3.3

min方法,返回兩個參數值中較小的值

double d1 = Math.min(3.3, 5.5); //d1的值爲3.3
double d2 = Math.max(-3.3, -5.5); //d2的值爲-5.5

pow方法,返回第一個參數的第二個參數次冪的值

double d1 = Math.pow(2.0, 3.0); //d1的值爲 8.0
double d2 = Math.pow(3.0, 3.0); //d2的值爲27.0

round方法,返回參數值四捨五入的結果

double d1 = Math.round(5.5); //d1的值爲6.0
double d2 = Math.round(5.4); //d2的值爲5.0

random方法,產生一個大於等於0.0且小於1.0的double小數

double d1 = Math.random();

Arrays類

此類包含用來操做數組(好比排序和搜索)的各類方法。須要注意,若是指定數組引用爲 null,則訪問此類中的方法都會拋出空指針異常NullPointerException。

經常使用方法

sort方法,用來對指定數組中的元素進行排序(元素值從小到大進行排序)

//源arr數組元素{1,5,9,3,7}, 進行排序後arr數組元素爲{1,3,5,7,9}
int[] arr = {1,5,9,3,7};
Arrays.sort( arr );

toString方法,用來返回指定數組元素內容的字符串形式

int[] arr = {1,5,9,3,7};
String str = Arrays.toString(arr); // str的值爲[1, 3, 5, 7, 9]

binarySearch方法,在指定數組中,查找給定元素值出現的位置。若沒有查詢到,返回位置爲-1。要求該數組必須是個有序的數組。

int[] arr = {1,3,4,5,6};
int index = Arrays.binarySearch(arr, 4); //index的值爲2
int index2= Arrasy.binarySearch(arr, 2); //index2的值爲-1

Arrays類的方法練習

練習一:定義一個方法,接收一個數組,數組中存儲10個學生考試分數,該方法要求返回考試分數最低的後三名考試分數。

複製代碼
  public static int[] method(double[] arr){
        Arrays.sort(arr); //進行數組元素排序(元素值從小到大進行排序)
        int[] result = new int[3]; //存儲後三名考試分數
        System.arraycopy(arr, 0, result, 0, 3);//把arr數組前3個元素複製到result數組中
        return result;
    }
複製代碼

大數據運算

BigInteger

java中long型爲最大整數類型,對於超過long型的數據如何去表示呢.在Java的世界中,超過long型的整數已經不能被稱爲整數了,它們被封裝成BigInteger對象.在BigInteger類中,實現四則運算都是方法來實現,並非採用運算符.
BigInteger類的構造方法:

構造方法中,採用字符串的形式給出整數
四則運算代碼:

複製代碼
  public static void main(String[] args) {
//大數據封裝爲BigInteger對象
        BigInteger big1 = new BigInteger("12345678909876543210");
        BigInteger big2 = new BigInteger("98765432101234567890");
//add實現加法運算
        BigInteger bigAdd = big1.add(big2);
//subtract實現減法運算
        BigInteger bigSub = big1.subtract(big2);
//multiply實現乘法運算
        BigInteger bigMul = big1.multiply(big2);
//divide實現除法運算
        BigInteger bigDiv = big2.divide(big1);
    }
複製代碼

BigDecimal

在程序中執行下列代碼,會出現什麼問題?

System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);

double和float類型在運算中很容易丟失精度,形成數據的不許確性,Java提供咱們BigDecimal類能夠實現浮點數據的高精度運算

構造方法以下:

建議浮點數據以字符串形式給出,由於參數結果是能夠預知的
實現加法減法乘法代碼以下:

複製代碼
 public static void main(String[] args) {
//大數據封裝爲BigDecimal對象
        BigDecimal big1 = new BigDecimal("0.09");
        BigDecimal big2 = new BigDecimal("0.01");
//add實現加法運算
        BigDecimal bigAdd = big1.add(big2);

        BigDecimal big3 = new BigDecimal("1.0");
        BigDecimal big4 = new BigDecimal("0.32");
//subtract實現減法運算
        BigDecimal bigSub = big3.subtract(big4);

        BigDecimal big5 = new BigDecimal("1.105");
        BigDecimal big6 = new BigDecimal("100");
//multiply實現乘法運算
        BigDecimal bigMul = big5.multiply(big6);
    }
相關文章
相關標籤/搜索