大數據分析技術之JAVA基礎(五續其他常用類)

寫在開頭:今天繼續給大家分享關於JAVA的一些基礎實用類。

學習內容安排

JAVA基礎課程學習:數據類型(一)、運算符表達式和程序結構控制(二)、面向對象基礎:類與對象和接口(三)、面向對象基礎:繼承抽象多態封裝(四)、異常類和常用實用類(五)、組件和事件處理(六)、IO和JDBC(七)、泛型和集合函數(八)。

五、異常類和常用實用類

今天主要分享的類別有StringBuffer、Date、Calendar、Math、BigInteger、Random,下面我們結合例子分別對每個類別的日常用法進行分享。
4.其他常用類
4.1StringBuffer
經常我們需要對String字符串進行增刪改的時候都會感覺會挺麻煩的,不太方便,但如果使用StringBuffer類的話,就可以直接調用方法進行操作。一般的操作有append增加字符串,insert插入、reverse轉置、delete相關刪除、repalce替換。我們會在程序中解釋具體的類的使用。
先來展示append和單字符提取charAt的方法,

package test4;

public class Csdn2_1 {
    public static void main(String[] args) {
        StringBuffer s = new StringBuffer("明曦君");
        
        //StringBuffer append(String s)\(int n)\(Object o)... 
        System.out.println("1.append添加字符串");
        s.append("加油!");
        System.out.println(s);
        
        //public chat charAt(int n); public voud setCharAt(int n, char ch)
        System.out.println(" ");
        System.out.println("2.charAt提取某個位子上得單個字符並替換該字符");
        char c = s.charAt(0);
        System.out.println(c);
        s.setCharAt(0, '晨'); //char類型需要使用單引號
        System.out.println(s);
    }
}
run:
1.append添加字符串
明曦君加油!
 
2.charAt提取某個位子上得單個字符並替換該字符
明
晨曦君加油!

然後我們來介紹一下insert、reverse以及delete和replace方法,

package test4;

public class Csdn2_1 {
    public static void main(String[] args) {
        StringBuffer s = new StringBuffer("明曦君");
        System.out.println("3.插入字符串");
        s.insert(2, "GoGo");
        System.out.println(s);
        
        //public StringBuffer reverse()
        System.out.println(" ");
        System.out.println("4.將字符串進行翻轉");
        s.reverse();
        System.out.println(s);
        
        //StringBuffer delete(int startIndex, int endIndex)
        System.out.println(" ");
        System.out.println("5.對字符串進行刪除");
        s.delete(1, 5); //不包括右邊界
        System.out.println(s);
        s.deleteCharAt(0);//刪除第一個字符
        System.out.println(s);
        
        //StringBuffer replace(int startIndex, int endIndex, String str)
        System.out.println(" ");
        System.out.println("6.對字符串進行替換");
        s.replace(0, 2, "明曦");
        System.out.println(s);
    }
}
run:
3.插入字符串
明曦GoGo君
 
4.將字符串進行翻轉
君oGoG曦明
 
5.對字符串進行刪除
君曦明
曦明
 
6.對字符串進行替換
明曦

當然如果有時候就是想用String類型,但又想進行操作,我們可以先對String轉換爲StringBuffer然後再轉換回String。

package test4;

public class Csdn2_1 {
    public static void main(String[] args) {
        String ss = "明曦君";
        StringBuffer sss = new StringBuffer(ss);
        sss.reverse();
        ss = sss.toString();
        System.out.println(ss);
    }
}
run:
君曦明

以上就是對StringBuffer的一些方法的展示,下面我們來介紹一些其他類別。


4.2Date與Calendar類
Date()的方法可以獲取本地當前的時間,返回的就是一串時間,而當我們想去了解這個時間關於日曆的內容,比如今天是第幾周,第幾個月這種信息,那我們就需要結合Calendar類進行操作,

package test4;

import java.util.Calendar;
import java.util.Date;

public class Csdn2_1 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        Date d = new Date();
        System.out.println(d);
        c.setTime(d);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int dayYear = c.get(Calendar.DAY_OF_YEAR);
        System.out.println("現在是多少年? "+year);
        System.out.println("現在是第幾個月? "+month);
        System.out.println("現在是一年的第幾天? "+dayYear);
    }
}
Sun Mar 15 11:22:29 CST 2020
現在是多少年? 2020
現在是第幾個月? 2
現在是一年的第幾天? 75

這裏需要注意的是對於月份的輸出,雖然現在是3月但輸出的是2,這是因爲這裏使用的是羅馬教皇日曆是從0到11的,所以我們在輸出時需要加1。


4.3Math類
Math類在java.lang包中,其中包括很多科學計算的類方法,可以直接通過類名進行調用,還有兩個靜態常量一個是E一個是PI,我們對Math包的一些常用類方法進行了總結如下表,

類方法 返回值
public static long abs(double a) 返回a的絕對值
public static double max(double a, double b) 返回a、b的最大值
public static double min(double a, double b) 返回a、b的最小值
public static double random() 產生一個(0,1)之間的隨機數
public static double pow(double a, double b) 返回a的b次冪
public static double sqrt(double a) 返回a的平方根
public static double log(double a) 返回a的對數
public static double sin(double a) 返回正弦值
public static double asin(double a) 返回反正弦值

下面我們來展示一下各個函數的使用例子,

package test4;

public class Csdn2_1 {
    public static void main(String[] args) {
        System.out.println(Math.E);         //返回E
        System.out.println(Math.PI);        //返回pi
        int a = -1, b = 2;
        System.out.println(Math.abs(a));    //返回a的絕對值
        System.out.println(Math.max(a, b)); //返回a和b的最大值
        System.out.println(Math.min(a, b)); //返回a和b的最小值
        double random = Math.random();
        System.out.println(random);         //返回0-1的隨機數
        System.out.println(Math.pow(a, b)); //返回a的b次方
        System.out.println(Math.sqrt(b));   //返回b的平方根
        System.out.println(Math.log(b));    //返回b的對數
        System.out.println(Math.sin(Math.PI/2));//返回pi/2的sin值"+dayYear);
    }
}
run:
2.718281828459045
3.141592653589793
1
2
-1
0.5653616285177047
1.0
1.4142135623730951
0.6931471805599453
1.0

4.4BigInteger類
當遇到很多維的數值時,我們的基礎類型long這些都不管用,甚至是Long類也不管用的時候就可以調用BigInteger,這個是java.math裏面的類提供任意精度的整數運算。方然運用BigInteger類了之後就不能直接使用加減乘除來對進行運算了,需要使用方法來進行操作。使用public BigInreger(String val)來創建一個大整數,

類名 返回
public BigInteger add(BigInteger val) 大整數求和
public BigInteger subtract(BigInteger val) 對參數大整數求差
public BigInteger multiply(BigInteger val) 對參數大整數求積
public BigInteger divide(BigInteger val) 對參數大整數求商
public BigInteger remainder(BigInteger val) 對參數大整數求餘
public BigInteger pow(int a) 返回當前大整數的a次冪
public int compareTo(BigInteger val) 比較大小,返回1,-1,0
public String toString() 十進制字符串表示
public String toString(int p) p進制字符串表示

我們通過計算一個實例來展示我們的代碼,我們計算6的二次方除以3然後再加上5的結果與18比大小,

package test4;
import java.math.BigInteger;

public class Csdn2_1 {
    public static void main(String[] args) {
        BigInteger k = new BigInteger("6");
        BigInteger c = new BigInteger("3");
        BigInteger d = new BigInteger("5");
        BigInteger f = new BigInteger("18");
        System.out.println("計算k的2次方除以c加d並與f比大小");   	  	
        System.out.println(k.pow(2).divide(c).add(d).compareTo(f));
    }
}
run:
計算k的2次方除以c加d並與f比大小
-1

4.5Random類
在Math類中我們已經講了可以使用math.random()的方法來調取(0,1)的隨機數,那麼JAVA提供了一個更加靈活的獲得隨機數的辦法就是Random類,我們可以使用public Random()或public Radnom(long seed)來創建Random對象,然後調用nextInt()或者其他方法來生成隨機數,下面給幾個例子。
這裏需要注意的是我們可以使用nextBytes來使得byte數組中的值填充隨機數,這種方法可以快速構造一個小的隨機數組。

package test4;
import java.util.Random;

public class Csdn2_1 {
    public static void main(String[] args) {
        Random dd = new Random();
        int x = dd.nextInt(100);//0到100
        System.out.println(x);
        Boolean f = dd.nextBoolean();
        System.out.println(f);//隨機生成布爾值
        byte c[] = new byte[10];//使用byte就是可以隨機替換byte數組裏的值
        dd.nextBytes(c);
        for(byte b: c){
            System.out.print(b);
        }
    }
}
run:
88
true
76-60-1920-3875-28883-75

結語
以上就是JAVA對於字符串處理的常見類,已經一些比較實用類的介紹通過這些類的使用可以大大提高我們編程的效率。
謝謝閱讀。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章