1. 程式人生 > >Java基礎之常用類

Java基礎之常用類

per fse 出現 repl 四舍五入 stat 方式 pattern 第一次

一、String

1.多個字符組成的一串數據,它可以和字符數組進行相互轉換

2.構造方法:
  public String ( ) 空構造
  public String (byte[ ] bytes) 把字節數組轉成字符串
  public String (byte[ ] bytes,int offset,int length) 把字節數組的一部分轉成字符串
  public String (char[ ] value) 把字符數組轉成字符串
  public String (char[ ] value,int offset,int count) 把字符數組的一部分轉成字符串
  public String (String original) 把字符串常量值轉成字符串

3.方法:
  判斷功能
    boolean equals(Object obj) 比較字符串的內容是否相同,區分大小寫
      例 s1.equals(s2) s1和s2比較
    boolean equalsIgnoreCase(String str) 比較字符串內容是否相同,忽略大小寫
      例 s1.equals(s2) s1和s2比較,註意區分大小寫
    boolean contains(String str) 判斷大字符串中是否包含小字符串
      例 s1.contains("hello") 判斷s1中有沒有hello這個字符串
    boolean startsWith(String str) 判斷字符串是否以某個指定的字符串開頭
      例 s1.startWith("h") 判斷s1中是否以h開頭
    boolean endsWith(String str) 判斷字符串是否以某個指定的字符串結尾
      例 s1.endWith("s") 判斷s1中是否以s結尾
    boolean isEmpty() 判斷字符串是否為空
      例 s1.isEmpty() 判斷s1是否為空字符串

  獲取功能
    int length() 獲取字符串的長度
     例 s.length()
    char charAt(int index) 獲取指定位置索引的字符
      例 s.charAt(7) 獲取第七個位置的字符(從0開始)
    int indexOf(int ch) 返回指定字符在此字符串中第一次出現的索引
      例 s.indexOf("c") 獲取 c 第一次出現的位置
    int indexOf(String str) 返回指定字符串在此字符串中第一次出現的索引
      例 s.indexOf("cake") 獲取 cake 第一次出現的位置
    int indexOf(int ch,int fromIndex) 返回指定字符在此字符串中從指定位置後第一次出現處的索引
      例 s.indexOf("c",4) 從第4個索引後獲取 c 的索引
    int indexOf(String str,int fromIndex) 返回指定字符串在此字符串中從指定位置後第一次出現處的索引
      例 s.indexOf("cake",4) 從第4個索引後獲取 cake 的索引
    String substring(int start) 從指定位置截取字符串,默認到結尾
      例 s.substring(5) 從第5個位置截取字符串
    String substring(int start,int end) 從指定位置開始到結束截取字符串
      例 s.substring(5,8) 從第5個位置截取字符串到第8個結束,不包括第8個字符。(包左不包右)

  轉換功能
    byte[ ] getBytes( ) 把字符串轉換為字節數組。
      例 byte[ ] bys = s.getBytes( );
    char[ ] toCharArray( ) 把字符串轉換為字符數組
      例 char[ ] cha = s.toCharArray();
    static String valueOf(char[ ] chs) 把字符數組轉成字符串。
      例 String ss = String.valueOf(cha);
    static String valueOf(int i) 把int類型的數據轉成字符串
      例 int y=100;
       String s2= String.valueOf(y);
    String toLowerCase( ) 把字符串轉成小寫
      例 String s1=s.toLowerCase
    String toUpperCase() 把字符串轉成大寫
      例 String s1=s.toUpperCase
    String concat(String str) 把字符串拼接
      例 s1.concat(s2) 把s1和s2拼接


  其他功能
    String replace(char old, char new) 替換字符串中的某一個字符
      例 s1.replace("p","u") 把s1中的所有p字符替換成u字符
    String replace(String old, String new) 替換字符串中的字符串
     例 s1.replace("hello","feiji") 把s1中的hello替換成feiji
    String trim() 去除字符串兩端空格
     例 s1.trim();
    int compareTo(String str) 按字典順序比較兩 個字符串
     例 s1.compareTo(s2);
     把s1和s2比較,一樣返回0。
    int compateToIgnoreCase(String str) 按字典順序比較兩個字符串,區分大小寫
      例 同上

二、StringBuffer(是同步的,數據安全,效率低)/StringBuilder(單線程使用,不同步,效率高)

1.線程安全的可變字符串。


2.構造方法
  public StringBuffer() 無參構造方法。
  public StringBuffer(int capacity) 指定容量的字符串緩沖區對象。
  public StringBuffer(String str) 指定字符串內容的字符串緩沖區對象。


3.方法
A:添加功能
    public StringBuffer append(String str) 添加任意類型到字符串杯子中
    public StringBuffer insert(int offset,String str) 在指定位置插入任意類型的數據到杯子中

B: 刪除功能
    public StringBuffer deleteCharAt(int index) 刪除指定位置的一個字符
    public StringBuffer delete(int start,int end) 刪除指定區間的所有字符(包左不包右)

C: 替換功能
    public StringBuffer replace(int start,int end,String str) 替換指定區間的字符串(包左不包右)

D: 反轉功能
    public StringBuffer reverse() 反轉字符串,例 abc--cba

E: 截取功能(註意返回值是String類型的)
    public String substring(int start) 截掉字符串(截掉輸入參數之前的所有字符串)
    public String substring(int start,int end) 截掉區間的字符串(包左不包右)
    public int capacity() 返回當前容量。
    public int length() 返回長度(字符數)。

三、對比StringStringBuffer拼接字符串的效率

package text;

public class Text {
    public static void main(String[] args) {
        String s = "";
        long start_s = System.currentTimeMillis();
        for(int i = 0;i < 10000;i++){
            s += "aaaa";
        }
        long end_s = System.currentTimeMillis();
        System.out.println("String拼接N次用的時間" + (end_s - start_s) + "ms" );
        
        StringBuffer ss = new StringBuffer();
        long start_ss = System.currentTimeMillis();
        for(int i = 0;i < 10000;i++){
            ss.append("aaaa");
        }
        long end_ss = System.currentTimeMillis();
        System.out.println("StringBuffer拼接N次用的時間" + (end_ss - start_ss) + "ms" );
    }
}

技術分享

四、Math

1.Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。

2.成員方法
  public static int abs(int a) 返回 int 值的絕對值。
  public static double ceil(double a) 向上取整
  public static double floor(double a) 向下取整
  public static int max(int a,int b) 比較兩個數的最大值
  public static double pow(double a,double b) a的b次冪
  public static double random() 隨機數
  public static int round(float a) 四舍五入
  public static double sqrt(double a) 正平方根

例子:因為都為靜態的,所以直接Math.方法用即可

package text;

public class Text {
    public static void main(String[] args) {
        System.out.println(Math.E);  //輸出值為:2.718281828459045
        System.out.println(Math.PI); //輸出值為:3.141592653589793
        System.out.println(Math.abs(-123));//輸出值為:123,絕對值
    }
}

五、Random

1.此類用於產生隨機數

2.構造方法

  public Random() 沒有給種子,用的是默認種子,是當前時間的毫秒值。
    例 Random r = new Random();
  public Random(long seed) 給出指定的種子,給出種子後每次得到的隨機數是相同的
    例 Random r = new Random(1201);

3.成員方法

  public int nextInt() 返回的是int範圍內的隨機數
    例 r.nextInt() 返回一個int範圍內的隨機數
  public int nextInt(int n) 返回的是【0,n】範圍內的隨機數
    例 r.nextInt(10) 返回0到10以內的隨機數

例子1:

package text;

import java.util.Random;

public class Text {
    public static void main(String[] args) {
        Random r = new Random();
        int a = r.nextInt(10);
        System.out.println(a);//返回值為0~10內的隨機一個數
    }
}

例子2:

package text;

import java.util.Random;

public class Text {
    public static void main(String[] args) {
        for(int i=0;i<10;i++){   //for循環,循環10次,每次輸出一個0~10的隨機數
            Random r = new Random();
            int a = r.nextInt(10);
            System.out.println(a);
        }    
    }
}

技術分享

六、Date

1.Date 表示特定的瞬間,精確到毫秒

2.構造方法

  public Date() 根據當前的毫秒值創建日期對象
  public Date(long date) 根據給定的毫秒值創建日期對象

3.成員方法
  public long getTime() 獲取當前時間
  public void setTime(long time) 設置時間

七、DateFormat

1.DateFormat 是日期/時間格式化子類的抽象類,它以與語言無關的方式格式化並解析日期或時間。
是抽象類,所以使用其子類SimpleDateFormat

2.SimpleDateFormat(可以把日期轉換成String類型)

3.構造方法
  public SimpleDateFormat() 默認模式
  public SimpleDateFormat(String pattern)
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss")
4.成員方法
  public final String format(Date date) 把日期格式轉換成String類型
  public Date parse(String source) 把給定的字符串解析成日期格式

例子:

package text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatUtil {
    public static String dateToString(Date date){  //見名知意,日期轉字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        return sdf.format(date);    
    }
    
    public static Date stringToDate(String str){   //見名知意,字符串轉日期
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            d = sdf.parse(str);
        } catch (ParseException e) {    
            e.printStackTrace();
        }
        return d;
    }
}
package text;

import java.util.Date;

public class Text {
    public static void main(String[] args) {
        
        Date d = new Date();
        System.out.println(DateFormatUtil.dateToString(d)); //輸出值為:2017年07月23日 14:55:23
        
        String ss = "2015-07-23";
        System.out.println(DateFormatUtil.stringToDate(ss)); //輸出值為:Thu Jul 23 00:00:00 CST 2015
    }
}

八、Calendar

1.Calendar 類是一個抽象類,它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日歷字段之間的轉 換提供了一些方法,並為操作日歷字段(例如獲得下星期的日期)提供了一些方法。

2.成員方法
  public static Calendar getInstance() 獲取當前時間
  Calendar c = Calendar.getInstance;

  public int get(int field) 返回給定日歷字段的值。
  public void add(int field,int amount) 根據給定的日歷字段和對應的時間,來對當前的日歷進行操作
  public final void set(int year,int month,int date) 設定當前的日歷時間

Java基礎之常用類